【问题标题】:pandas.read_html returns wrong table contentspandas.read_html 返回错误的表格内容
【发布时间】:2021-03-10 20:31:09
【问题描述】:

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

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

第一个表格如下所示:

以下是我的代码:

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

如你所见,刮出来的表是完全错误的。

我怎样才能正确刮桌子?

提前感谢您的帮助:-)

【问题讨论】:

    标签: python pandas web-scraping


    【解决方案1】:

    让我们看看 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

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 2015-01-22
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多