首页 > 解决方案 > Python requests.get():如果出现 Internet 连接错误,如何打印消息?

问题描述

我正在尝试打印错误消息:Oops! Houston, we have a problem!如果由于某种原因在尝试使用requests.get().

我已经编写了下面的代码,但Oops! Houston, we have a problem!即使在尝试发送请求时没有问题,它也总是会打印出来。

try:
    page = requests.get(URL, headers=headers)
    print("SUCCESS!")

except (requests.exceptions.Timeout, requests.exceptions.TooManyRedirects, requests.exceptions.RequestException, requests.exceptions.ConnectionError, requests.exceptions.HTTPError,):
    print("Oops! Houston, we have a problem!")

如果您能给我一些反馈,我将不胜感激!

标签: pythonpython-requeststry-except

解决方案


I believe requests.exceptions.RequestException is catching all the responses, as it worked when I removed this from the except


推荐阅读