python - 如何从网页中提取数据并将其转换为正确的 Pandas 数据框?
问题描述
例如,这里是一个地址:https
://pesdb.net/pes2021/?id=
44379 似乎没有api调用(我对此很陌生,但我在网络监视器中检查了XHR并且没有相关的json调用)。
解决方案
这里有一个如何解析 html 表的示例,仅使用 Pandas/requests 库。
根据最新的文档,您可以跳过该答案中的 requests 调用,但您需要安装依赖项:
pip install lxml html5lib beautifulsoup4
然后你可以做这样的事情:
df_list = pd.read_html('https://pesdb.net/pes2021/?id=44379') # this parses all the tables in webpages to a list
df = df_list[0] # the first table on the page
print(df) # this is your dataframe!
一般来说,Beautiful Soup 4 是最流行的用于网页抓取的 Python 库。
你可以在这里阅读一些例子
或者,您可以对站点执行 GET 请求并手动解析响应。(最困难/毫无意义)
推荐阅读
- laravel - Laravel 5.7,带有驱动程序护照的 API,无法在登录时添加验证
- javascript - 如何从 webgl 中的前/左/上向量创建投影矩阵
- emacs - 如何明确指定自动压缩模式应处理哪些存档文件?
- arrays - 在 Postman 中取消嵌套 JSON 响应
- c++ - 指向不同大小的成员数组的指针
- repast-simphony - 在 REPAST 中按季度/月汇总输出
- r - 将一行的一个值与同一列的所有行的平均值进行比较
- docker - 我想创建多个容器,然后创建不同的 pod 并将容器放入 pod
- python - match.call() 有什么作用?
- python-3.6 - 执行 cx_Freeze setup.py 后,cmd 中没有任何反应