作为标题,我尝试使用 read_html
但出现以下错误:
In [17]:temp = pd.read_html('C:/age0.html',flavor='lxml')
File "<string>", line unknown
XMLSyntaxError: htmlParseStartTag: misplaced <html> tag, line 65, column 6
我做错了什么?
更新01
HTML 在顶部包含一些 javascript,然后是一个 html 表格。我使用 R 通过 XML 包解析 html 来处理它,给我一个数据框。我想用 python 做,我应该在给 pandas 之前使用其他东西,比如 beautifulsoup 吗?
最佳答案
我认为您使用像 beautiful soup 这样的 html 解析器是在正确的轨道上。 pandas.read_html() 读取 html 表格而不是 html 页面。
你会想做这样的事情......
from bs4 import BeautifulSoup
import pandas as pd
table = BeautifulSoup(open('C:/age0.html','r').read()).find('table')
df = pd.read_html(table) #I think it accepts BeatifulSoup object
#otherwise try str(table) as input
关于python - 使用 Pandas 读取下载的html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25056120/