当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正文。
答案 0 :(得分:10)
抓住它并使用它,好像它是一个回应:
try:
conn = urllib.request.urlopen(...)
# ...
except urllib.error.HTTPError as error:
data = error.read()
# data contains the content
答案 1 :(得分:1)
就像正常的过程一样。
err.read()