Converting html table to a pandas dataframe

巧了我就是萌 提交于 2019-12-09 21:34:13

问题


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 

It just displays this:

0       S.No                                     ...

But if I do;

for df in dfs:
    print df

It outputs the table..

How can I use pd.Dataframe to scrape the table?


回答1:


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

Output:



来源:https://stackoverflow.com/questions/42128760/converting-html-table-to-a-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!