'HTTPResponse' object 在尝试捕获 404 错误时没有属性 'type'
'HTTPResponse' object has no attribute 'type' when trying to catch 404 errors
我有一些代码可以从 API 中提取不完整的 URL 并将它们附加到基础 URL 中。我正在尝试扩展它以测试每个 URL 以确保它在打印到屏幕之前不会导致 404。
我查看了有关如何将 urllib 与 python3 一起使用的其他答案,并认为我已经正确地完成了所有操作,但是,我在标题中遇到了错误。
testurl
是我的请求,resp
是我的回应。这是我使用的代码:
testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")
我错过了什么?
完整的错误输出:
Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'
编辑:
由于Bruno的回答指出问题后,我改为尝试以下代码:
try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")
但是,这会导致根本不打印任何内容。
这里:
testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
第一行调用 urlopen
并将结果(HTTPResponse
对象)绑定到 testurl
。然后在 try
块中,您第二次调用 urlopen
, 将 HTTPResponse 对象作为参数 - 这当然是无效的。
编辑:
使用您编辑的代码,即:
try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")
"it worked" 仅在引发 HTTPError 且不是 404 时才会打印 - else
子句与 if e.code == 404
匹配。所以当然,如果没有错误,那么什么也不会打印出来。
你想要的是:
try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)
所以在你的情况下,它看起来像:
try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))
请注意,此处的 else
子句与 try
子句匹配。
我有一些代码可以从 API 中提取不完整的 URL 并将它们附加到基础 URL 中。我正在尝试扩展它以测试每个 URL 以确保它在打印到屏幕之前不会导致 404。
我查看了有关如何将 urllib 与 python3 一起使用的其他答案,并认为我已经正确地完成了所有操作,但是,我在标题中遇到了错误。
testurl
是我的请求,resp
是我的回应。这是我使用的代码:
testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
except urllib.error.HTTPError as e:
if e.code == 404:
blah = 1
else:
print("it worked!")
我错过了什么?
完整的错误输出:
Traceback (most recent call last):
File "imgtst.py", line 27, in <module>
resp = urllib.request.urlopen(testurl)
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 517, in open
protocol = req.type
AttributeError: 'HTTPResponse' object has no attribute 'type'
编辑:
由于Bruno的回答指出问题后,我改为尝试以下代码:
try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")
但是,这会导致根本不打印任何内容。
这里:
testurl=urllib.request.urlopen("http://www.google.com")
try:
resp = urllib.request.urlopen(testurl)
第一行调用 urlopen
并将结果(HTTPResponse
对象)绑定到 testurl
。然后在 try
块中,您第二次调用 urlopen
, 将 HTTPResponse 对象作为参数 - 这当然是无效的。
编辑:
使用您编辑的代码,即:
try:
resp = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
if e.code == 404:
print("error")
else:
print("it worked")
"it worked" 仅在引发 HTTPError 且不是 404 时才会打印 - else
子句与 if e.code == 404
匹配。所以当然,如果没有错误,那么什么也不会打印出来。
你想要的是:
try:
result = something_that_may_raise(...)
except SomeExceptionType as e:
handle_the_error
else:
do_something_with(result)
所以在你的情况下,它看起来像:
try:
response = urllib.request.urlopen("http://www.google.com")
except urllib.error.HTTPError as e:
print("error code {}".format(e.code))
else:
print("it worked: {}".format(response))
请注意,此处的 else
子句与 try
子句匹配。