【发布时间】:2018-12-22 12:07:56
【问题描述】:
我正在尝试导入一系列 HTML 文件,其中包含我保存在工作目录中的新闻文章。我使用一个 HTML 文件开发了代码,并且运行良好。但是,我已经修改了代码以导入多个文件。
从下面的代码可以看出,我使用的是 pandas 和 pd.read_html()。它不再导入任何文件并给我错误代码“ValueError: No tables found”。
我尝试过使用不同类型的 HTML 文件,所以这似乎不是问题。我还更新了我正在使用的所有软件包。我在 Anaconda Navigator 中使用 OSX 和 Python 3.6 和 Pandas 0.20.3。
以前可以,现在不行了。我究竟做错了什么?
任何提示或线索将不胜感激。
import pandas as pd
from os import listdir
from os.path import isfile, join, splitext
import os
mypath = 'path_to_my_wd'
raw_data = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and splitext(f)[1]=='.html')]
news = pd.DataFrame()
for htmlfile in raw_data:
articles = pd.read_html(join(mypath, htmlfile), index_col=0) #reads file as html
data = pd.concat([art for art in articles if 'HD' in art.index.values],
axis=1).T.set_index('AN')
data_export = pd.DataFrame(data, columns=['AN', 'BY', 'SN', 'LP', 'TD'])
#selects columns to export
news = news.append(data_export)
【问题讨论】:
-
我认为你需要在
pd.read_html中使用join(mypath, raw_data) -
感谢@stellasia 的建议!但是,我仍然无法使其工作。我注意到我上传了修改后的代码版本。原版在
pd.read_html中有join(mypath, htmlfile),但这并没有什么不同。我已经修改了代码。还有其他建议吗? -
其他建议是使用
columns参数创建news数据帧,就像使用data_export告诉熊猫数据帧的结构一样。 -
再次感谢,真的很感谢@stellasia!但仍然无法正常工作 - 非常令人沮丧。
标签: python-3.x pandas html-table