[英]How to catch http error 404 with urllib3?
I'm requesting a file from an API.我从 API 请求文件。 If the file does't exist, I get a 404.
如果文件不存在,我会得到 404。
I'm trying to handle this, using urllib3
.我正在尝试使用
urllib3
来处理这个问题。
I found a lot of great, but outdated (~10 years old), documentation how to do this with with urllib
and urllib2
.我发现了很多很棒但已过时(约 10 年)的文档,如何使用
urllib
和urllib2
来做到这一点。
How does this work in urllib3
?这在
urllib3
中如何工作?
All I found in their docs was this我在他们的文档中发现的就是这个
try:
http.request('GET', 'nx.example.com', retries=False)
except urllib3.exceptions.NewConnectionError:
print('Connection failed.')
You can simply look at the status code:您可以简单地查看状态码:
import urllib3
http = urllib3.PoolManager()
r = http.request("GET", "httpbin.org/status/404")
if r.status == 404:
print("404!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.