python,urllib2,因 404 错误而崩溃

标签 python beautifulsoup urllib2 http-status-code-403

我有一个程序可以从存储在数据库中的 url 中获取内容。我正在使用 beautifulsoupurllib2 来抓取内容。当我输出结果时,我发现程序在遇到(看起来像)403 错误时崩溃了。那么如何防止我的程序因 403/404 等错误而崩溃?

相关输出:

Traceback (most recent call last):
  File "web_content.py", line 29, in <module>
    grab_text(row) 
  File "web_content.py", line 21, in grab_text
    f = urllib2.urlopen(row)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

最佳答案

你可以用try/except包围请求,例如

try:
    urllib2.openurl(url)
except urllib2.HTTPError, e:
    print e

参见 http://www.voidspace.org.uk/python/articles/urllib2.shtml#handling-exceptions一些很好的例子和信息。

关于python,urllib2,因 404 错误而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117885/

相关文章:

python - "Out of Memory" Mechanize 错误

Python Pandas : Index a value and boolean comparison

Python __init__ 语法

python - 使用退避 ngrams 在 nltk 中标记 pos

python-3.x - 为什么我无法抓取此 HTML 的 'data-src' 属性内的所有内容

python - 如何将 Py3k HTTPResponse 转储到 json.load 中?

python - 将 azure blob 直接加载到 pandas 数据帧中的首选方法是什么

python - BeautifulSoup find_all 仅返回前 50 个标签

python - 使用 Python 解析网页的搜索结果

python - 使用 beautifulsoup 获取 div 中的 child 文本