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

Python 如何避免urllib2.httperror 404错误的程序终止并显示适当的消息,python,python-2.7,web-scraping,beautifulsoup,Python,Python 2.7,Web Scraping,Beautifulsoup,我正在从10万个系统URL(example.com/entry/1>example.com/entry/100000)中删除内容 但是,大约有10%的URL已被删除,这意味着当脚本访问它们时,它会给出错误“urllib2.httperror http error 404”并停止运行 我对python比较陌生,我想知道是否有这样的方法: 如果结果==错误: div_text=“missing”是Python引发的异常。您可以使用try/except块包装URL调用: try: # ...

我正在从10万个系统URL(example.com/entry/1>example.com/entry/100000)中删除内容

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

我对python比较陌生,我想知道是否有这样的方法:

如果结果==错误:

div_text=“missing”是Python引发的异常。您可以使用try/except块包装URL调用:

try:
    # ... put your URL open call here ... 
except urllib2.HTTPError:
    div_text = 'missing'
这样,如果再次遇到此异常,Python解释器将在该块内运行代码