问题
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