如果urllib.request.urlopen
不是200,我如何避免response.status_code
的例外情况?现在它根据请求状态提升URLError
或HTTPError
。
有没有其他方法可以使用python3基本库进行请求?
如果status_code != 200
?
答案 0 :(得分:17)
使用try except
,代码如下:
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://www.111cn.net /")
try:
response = urlopen(req)
except HTTPError as e:
# do something
print('Error code: ', e.code)
except URLError as e:
# do something
print('Reason: ', e.reason)
else:
# do something
print('good!')
答案 1 :(得分:0)
我从py3 docs找到了一个解决方案
>>> import http.client
>>> conn = http.client.HTTPConnection("www.python.org")
>>> # Example of an invalid request
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print(r2.status, r2.reason)
404 Not Found
>>> data2 = r2.read()
>>> conn.close()
答案 2 :(得分:0)
The docs指出,异常类型HTTPError
也可以视为HTTPResponse
。因此,您可以从错误响应中获取响应主体,如下所示:
import urllib.request
import urllib.error
def open_url(request):
try:
return urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
# "e" can be treated as a http.client.HTTPResponse object
return e
,然后使用如下:
result = open_url('http://www.stackoverflow.com/404-file-not-found')
print(result.status) # prints 404
print(result.read()) # prints page contents
print(result.headers.items()) # lists headers