覆盖 urllib2.HTTPError 或 urllib.error.HTTPError 并读取响应 HTML [英] Overriding urllib2.HTTPError or urllib.error.HTTPError and reading response HTML anyway

查看:45
本文介绍了覆盖 urllib2.HTTPError 或 urllib.error.HTTPError 并读取响应 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到HTTP 错误 500:内部服务器错误"响应,但我仍想读取错误 HTML 中的数据.

I receive a 'HTTP Error 500: Internal Server Error' response, but I still want to read the data inside the error HTML.

使用 Python 2.6,我通常使用以下方法获取页面:

With Python 2.6, I normally fetch a page using:

import urllib2
url = "http://google.com"
data = urllib2.urlopen(url)
data = data.read()

尝试在失败的 URL 上使用它时,出现异常 urllib2.HTTPError:

When attempting to use this on the failing URL, I get the exception urllib2.HTTPError:

urllib2.HTTPError: HTTP Error 500: Internal Server Error

我如何获取此类错误页面(有或没有 urllib2),同时它们都返回内部服务器错误?

How can I fetch such error pages (with or without urllib2), all while they are returning Internal Server Errors?

请注意,对于 Python 3,相应的异常是 urllib.error.HTTPError.

Note that with Python 3, the corresponding exception is urllib.error.HTTPError.

推荐答案

HTTPError 是一个类似文件的对象.你可以捕捉它,然后读取它的内容.

The HTTPError is a file-like object. You can catch it and then read its contents.

try:
    resp = urllib2.urlopen(url)
    contents = resp.read()
except urllib2.HTTPError, error:
    contents = error.read()

这篇关于覆盖 urllib2.HTTPError 或 urllib.error.HTTPError 并读取响应 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆