使用以下内容
import requests
URLS = [r"http://www.google.com/",
r"https://httpbin.org/status/404",
r"http://www.hajsdgkahsjd.com"]
import requests
for url in URLS:
print (requests.get(url).status_code)
# 200
# 404
# ConnectionError: HTTPConnectionPool(host='www.hajsdgkahsjd.com', port=80):
#Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection #object at 0x00000141C52BB108>:
#Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
我知道我可以编写一个 try/catch 并返回一个“404”我自己,但是当它找不到最后一个 URL 时为什么请求不返回一个 404 代码(因为它不存在)?