将html表转换为pandas数据框 [英] Converting html table to a pandas dataframe

查看:397
本文介绍了将html表转换为pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to import a html table from a website and to convert it into a pandas DataFrame. This is my code:

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 刮擦桌子?

推荐答案

给定网址上的HTML表是用javascript呈现的. pd.read_html()不支持javascript呈现的页面.您可以尝试使用 dryscrape ,如下所示:

HTML table on the given url is javascript rendered. pd.read_html() doesn't supports javascript rendered pages. You can try with dryscrape like so:

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

输出:

这篇关于将html表转换为pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆