将 html 表转换为 pandas 数据框

2023-12-28

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

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.数据框刮桌子?


给定 url 上的 HTML 表是由 javascript 呈现的。pd.read_html()不支持 javascript 渲染页面。你可以尝试使用dryscrape https://dryscrape.readthedocs.io/en/latest/像这样:

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()

Output:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 html 表转换为 pandas 数据框 的相关文章

随机推荐