python - 奇怪的 Pandas.read_html 错误

标签 python pandas beautifulsoup lxml

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

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

如评论中所述,由于某种原因,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”,但这没有任何改变。

最佳答案

如果其他人也遇到这个问题,那么当我将代码更改为:

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!

关于python - 奇怪的 Pandas.read_html 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44292314/

相关文章:

python - 如何将时间序列数据中所有观测值的最新月份值替换为上个月值?

python - 超出递归深度、pickle 和 BeautifulSoup

python - Matplotlib 和多处理 RuntimeError

javascript - 如何使用 Node.js 向 AWS 发出经过身份验证的请求?

来自 lxml html 的 Python 打印元素

python - BeautifulSoup 查找有条件的结果

python - Beautiful Soup 4 find_all 找不到 Beautiful Soup 3 找到的链接

python - Pygame 鼠标点击 |在音乐播放中切换播放暂停

json - 如何修复 `` `ValueError : Trailing data ``` during `` `pandas. read_json(... )`` `?

python - 使用 python 计算列中单词出现的次数