首页 > 解决方案 > 如何从网页中提取数据并将其转换为正确的 Pandas 数据框?

问题描述

例如,这里是一个地址:https
://pesdb.net/pes2021/?id= 44379 似乎没有api调用(我对此很陌生,但我在网络监视器中检查了XHR并且没有相关的json调用)。

标签: pythonpandasdataframe

解决方案


这里有一个如何解析 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 请求并手动解析响应。(最困难/毫无意义)


推荐阅读