如何避免程序终止urllib2.httperror 404错误并显示相应的消息

问题描述 投票:0回答:1

我正在从100k系统URL中抓取内容(example.com/entry/1> example.com/entry/100000)。

但是,大约10%的URL已被删除,这意味着当脚本到达它们时,它会给出错误“urllib2.httperror http error 404”并停止运行。

我对python比较陌生,想知道是否有办法做这样的事情:

if result == error:
    div_text = "missing"

这样循环可以继续到下一个URL,但请注意它失败了。

python python-2.7 web-scraping beautifulsoup
1个回答
1
投票

urllib2.HTTPError是Python提出的一个例外。您可以使用try / except块包装URL调用:

try:
    # ... put your URL open call here ... 
except urllib2.HTTPError:
    div_text = 'missing'

这样,如果再次遇到此异常,Python解释器将运行除块之外的代码。

© www.soinside.com 2019 - 2024. All rights reserved.