注意:您应该按照上面Ian 的回答response.raise_for_status()
中的描述进行(他是模块的维护者之一)。requests
你如何处理这一切取决于你认为什么是 HTTP 错误。有状态代码,但并非所有内容都200
必然意味着存在某种错误。
如您所见,请求库仅考虑 HTTP 响应的另一方面,不会引发异常。302
例如, HTTP 状态表示Found
,但响应不包含响应正文,而是包含Location
标题,而不是您需要遵循的标题才能获取您真正想要的资源。
因此,您需要查看response.status_code
并进行处理,同时使用try..except
. 当捕获那些你应该实际捕获的东西requests.exceptions.RequestException
时,因为这是模块引发的所有其他异常的基类。requests
所以这是一个演示所有三种情况的示例:
- 成功
200 OK
响应
- 成功的请求和响应,但状态不是
200
- 协议错误(无效架构)
import requests
test_urls = ['http://httpbin.org/user-agent',
'http://httpbin.org/status/404',
'http://httpbin.org/status/500',
'httpx://invalid/url']
def return_json(url):
try:
response = requests.get(url)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return "Error: Unexpected response {}".format(response)
json_obj = response.json()
return json_obj
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
return "Error: {}".format(e)
for url in test_urls:
print "Fetching URL '{}'".format(url)
print return_json(url)
print
输出:
Fetching URL 'http://httpbin.org/user-agent'
{u'user-agent': u'python-requests/2.1.0 CPython/2.7.1 Darwin/11.4.2'}
Fetching URL 'http://httpbin.org/status/404'
Error: Unexpected response <Response [404]>
Fetching URL 'http://httpbin.org/status/500'
Error: Unexpected response <Response [500]>
Fetching URL 'httpx://invalid/url'
Error: No connection adapters were found for 'httpx://invalid/url'
如果您得到一个成功的响应,也可能会引发一个异常response.json()
,但它根本不是 JSON - 所以您可能也想考虑这一点。
注意:该if not response.status_code // 100 == 2
位的工作方式如下://
运算符执行所谓的floor 除法,因此它向下舍入到下一个整数(这是/
Python 2.x 中的默认行为,但不是 Python 3.x,它更改/
为浮点除法)。所以status // 100 == 2
适用于所有2xx
代码。