我有一些代码用来抓取网页。该代码如下所示:
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/