我正试图编写一个从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)
您使用
.read_html
的方式将返回所有表的列表。你的桌子在索引3.read_html
调用URL,并使用BeautifulSoup解析引擎盖下的响应。您可以像在.read_csv
中一样更改解析、表名、传递头。查看.read_html了解更多详细信息对于速度,您可以使用
lxml
,例如pd.read_html(url, flavor='lxml')[3]
。默认情况下,使用第二慢的html5lib
。另一种口味是html.parser
。这是他们中最慢的使用BeautifulSoup实现此目的,下面的代码可以完美地工作
使用BeautifulSoup的另一个优点是,您的代码运行速度要快得多
相关问题 更多 >
编程相关推荐