【问题标题】:Bizzare Pandas.read_html errorBizzare Pandas.read_html 错误
【发布时间】:2017-11-01 16:53:29
【问题描述】:

我有一些用于抓取网页的代码。该代码如下所示:

for pages in pagesToScrape:
     print('test')
     url = 'http://myurl.com' + str(pages)
     page = pandas.read_html(url, attrs={'class': 'tableToRead'}, header = 0)  # Scrape web page
     print('hi')  # This is never printed for some reason

如 cmets 中所述,出于某种原因,pandas.read_html 行下的任何代码都不会执行,但我也没有收到错误消息。这段代码在 2 个月前工作,所以我想知道 lxml、BeautifulSoup4 或它们的依赖项之一是否发生了变化,因为网页根本没有改变。我还验证了我使用的 URL 是有效的。对于其他测试,我也尝试过:

for pages in pagesToScrape:
         print('test')
         url = 'http://myurl.com' + str(pages)
         page = pandas.read_html(url, attrs={'class': 'tableToRead'}, header = 0)  # Scrape web page
         print(page)  # Doesn't print anything
         fasidfoaisdf()  # This non-existent function does not throw an error ever either...

有没有人知道为什么会发生这种情况?我觉得至少我可以让不存在的函数抛出错误,但程序编译得很好,甚至每次都运行 for 循环打印测试。

Python v3.5.3

BeautifulSoup4 v4.6.0

bs4 v0.0.1

lxml v3.7.3

编辑:我还尝试从 read_html 函数调用中删除“header = 0”,但没有任何改变。

【问题讨论】:

  • 看起来您在循环周围的脚本中有“错误”的异常处理部分。而这种异常处理只是忽略了异常
  • 哇,做到了。我添加了一个 try / except 并让 except 打印出错误。我早些时候尝试过这样做,但由于某种原因从未想过尝试打印出特定的错误。猜猜这就是深夜对你的影响。我将为遇到此问题的其他人粘贴下面的完整解决方案。谢谢!

标签: python pandas beautifulsoup lxml


【解决方案1】:

如果其他人有这个问题,当我将代码更改为:

for pages in pagesToScrape:
     print('test')
     url = 'http://myurl.com' + str(pages)
     try:     
         page = pandas.read_html(url, attrs={'class': 'tableToRead'}, header = 0)  # Scrape web page
     except Exception as e:
         print(e)
     print('hi')  # This is never printed for some reason

我收到了正确的错误消息,在我的情况下是没有安装 html5lib。谢谢 MaxU!

【讨论】:

    猜你喜欢
    • 2017-09-21
    • 2011-06-18
    • 1970-01-01
    • 2011-02-08
    • 2011-03-25
    • 1970-01-01
    • 2015-01-22
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多