【问题标题】:I don't get the complete table of an html with read_html我没有使用 read_html 获得完整的 html 表
【发布时间】:2020-07-21 15:43:26
【问题描述】:

我试图使用 Panda 从网页上的表格中获取信息,但它不会向我提供所有信息和其他方式,我也不能。

import pandas as pd
calls_df = pd.read_html("https://google.com/covid19-map/?hl=es-419", index_col=1,
                        attrs={"class":"SAGQRd"})
df = pd.DataFrame(calls_df)
print(calls_df)

我尝试了其他链接的代码,如果他们从表格中获取信息,我的错误是什么?

【问题讨论】:

  • 请说明您的期望和实际得到的?
  • 您好,您只选择具有SAGQRd 属性的类,参数attrs 限制为{"class":"SAGQRd"}。您可以检查页面的 html 源代码以查看您想要的类并进行相应的调整。希望对你有帮助
  • 或者你可以删除你给pandasread_html的参数,你会得到一个包含两个DataFrames的列表,然后你可以从中提取你想要的信息。

标签: python pandas spyder


【解决方案1】:

jupyter_code_check

嘿,

我刚刚通过 jupyter 检查了您的代码,它对我有用。 你也在使用 jupyter 吗?可能是缓存有点满 :D 重新启动您的 IDE 或计算机

我的设置:

Python 3.7.4

名称:熊猫 版本:0.25.1

名称:jupyter 版本:1.0.0

【讨论】:

    【解决方案2】:

    pandas 可能不是获取 html 数据的最佳方式,请尝试从以下位置探索 BeautifulSoup 模块:https://www.crummy.com/software/BeautifulSoup/bs4/doc/

    要将html加载到df中,请尝试:

    `import pandas as pd
     from bs4 import BeautifulSoup
     soup = BeautifulSoup(html, "html.parser")
     table = soup.find('table', attrs={'class':'subs noBorders evenRows'})
     table_rows = table.find_all('tr')`
    
     `res = []
     for tr in table_rows:
          td = tr.find_all('td')
          row = [tr.text.strip() for tr in td if tr.text.strip()]
          if row:
              res.append(row)
    
     df = pd.DataFrame(res, columns=["Year", "Mintage", "Quality", "Price"])
     print(df)`
    

    【讨论】:

    • 我的问题是我无法从表中获取所有信息。现在,我可以看到表格只有 15 行,而且还有更多行。我测试了你的代码,仍然得到 15 行。
    • 我将不得不查看 html 以进一步解决这个问题......某处可能会有一些格式/类型的变化。虽然 BeautfulSoup 是一个功能强大的库,非常适合您的要求..请浏览文档,看看是否有帮助.. 请记住,它通过将数据调整为树来使用解析,因此您可以实际解析每个节点并查看确切位置您的数据丢失了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多