【发布时间】:2020-10-03 16:42:25
【问题描述】:
我正在尝试编写用于从http://goldpricez.com/gold/history/lkr/years-3 抓取数据的代码。我写的代码如下。该代码有效,并给了我预期的结果。
import pandas as pd
url = "http://goldpricez.com/gold/history/lkr/years-3"
df = pd.read_html(url)
print(df)
但结果是一些不需要的数据,我只想要表中的数据。请帮我解决这个问题。
Here I have added the image of the output with unwanted data (red circled)
【问题讨论】:
-
您可以随时对数据框进行切片以删除不需要的数据。或者,在使用 pandas 库之前使用 Beautiful soup 库解析 html。
-
read_html返回HTML源中每个表格的数据框列表,使用列表索引访问所需的数据框stackoverflow.com/questions/39710903/… -
您使用
pd.read_html是正确的。只需选择数据所在的正确索引 [3]。请参阅下面的答案
标签: python web-scraping