【问题标题】:'ValueError: No tables found': Python pd.read_html not loading input files“ValueError:未找到表”:Python pd.read_html 未加载输入文件
【发布时间】: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


【解决方案1】:

HTML 文件的格式略有不同,我需要将 sort=False 传递给 pd.concat()data = pd.concat([art for art in articles if 'HD' in art.index.values], sort=False, axis=1).T.set_index('AN') 这是 Pandas 0.23.0 版中的新功能。这样就解决了问题。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 2015-11-18
    • 2021-12-18
    • 2019-04-23
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多