【问题标题】:Converting html table to a pandas dataframe将 html 表转换为 pandas 数据框
【发布时间】:2017-02-09 05:16:28
【问题描述】:

我一直在尝试从网站导入 html 表格并将其转换为 pandas DataFrame。这是我的代码:

import pandas as pd
table = pd.read_html("http://www.sharesansar.com/c/today-share-price.html")
dfs = pd.DataFrame(data = table)
print dfs 

它只是显示这个:

0       S.No                                     ...

但如果我这样做了;

for df in dfs:
    print df

它输出表格..

如何使用 pd.Dataframe 来抓取表格?

【问题讨论】:

    标签: python pandas quantitative-finance


    【解决方案1】:

    给定 url 上的 HTML 表格是 javascript 呈现的。 pd.read_html() 不支持 javascript 呈现的页面。您可以像这样尝试dryscrape

    import pandas as pd
    import dryscrape
    
    s = dryscrape.Session()
    s.visit("http://www.sharesansar.com/c/today-share-price.html")
    df = pd.read_html(s.body())[5]
    df.head()
    

    输出:

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2019-05-14
      • 2021-03-22
      • 2020-09-02
      相关资源
      最近更新 更多