当python3.2抛出http时,如何获取HTTPError主体。

Traceback (most recent call last):
    File "C:\Users\RileyRen\Desktop\query_token.py", line 17, in <module>
    File "D:\Environment\Python32\lib\urllib\request.py", line 495, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
    urllib.error.HTTPError: HTTP Error 401: Unauthorized

python抛出HTTPError,但是我无法获取http正文。

最佳答案

捕获并使用它,就好像它是一个响应一样:

try:
    conn = urllib.request.urlopen(...)
    # ...
except urllib.error.HTTPError as error:
    data = error.read()
    # data contains the content

关于http - 当python3抛出HTTPError时如何获取http正文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17227185/

10-09 05:26