首页 > 解决方案 > pandas.read_html 返回错误的表格内容

问题描述

我尝试从以下位置刮取两张表(资产和负债):

https://www.marketwatch.com/investing/stock/aapl/financials/balance-sheet

第一个表如下所示: 在此处输入图像描述

以下是我的代码:

tables = pd.read_html("https://www.marketwatch.com/investing/stock/spg/financials/balance-sheet")

在此处输入图像描述

如您所见,刮掉的桌子是完全错误的。

我怎样才能正确刮桌子?

预先感谢您的任何帮助:-)

标签: pythonpandasweb-scraping

解决方案


让我们看看这个的 selenium,你也许可以用 bs4 和一些有趣的请求东西来做到这一点

from selenium import webdriver
import time

url = "https://www.marketwatch.com/investing/stock/spg/financials/balance-sheet"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(10)
tables = driver.find_elements_by_class_name("table")

tables[3].text.splitlines() # this looks to split the new line characters
tables[4].text.splitlines()

之后,我认为您可以分配键值对并以这种方式制作数据框,或者使用 numpy


推荐阅读