Python 3 urllib.request.urlopen

Bog*_*kiy 7 python exception urllib httprequest python-3.4

urllib.request.urlopen如果response.status_code不是200,我怎样才能避免例外?现在它提出URLErrorHTTPError基于请求状态.

有没有其他方法来使用python3基本库进行请求?

如果能获得响应头status_code != 200

thi*_*rou 20

使用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!')
Run Code Online (Sandbox Code Playgroud)

  • 那么在这种情况下如何获得标题? (3认同)

dav*_*idg 8

文档指出异常类型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
Run Code Online (Sandbox Code Playgroud)

然后使用如下:

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
Run Code Online (Sandbox Code Playgroud)


Bog*_*kiy 0

我从 py3 文档中找到了解决方案

>>> 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()
Run Code Online (Sandbox Code Playgroud)

https://docs.python.org/3/library/http.client.html#examples