简体   繁体   English

Python requests.get():如果出现 Internet 连接错误,如何打印消息?

[英]Python requests.get(): How to Print a Message if There is an Internet Connection Error?

I am trying to print the error message: Oops! Houston, we have a problem!我正在尝试打印错误消息: Oops! Houston, we have a problem! Oops! Houston, we have a problem! if for some reason there is any internet connection problem when trying to use requests.get() .如果由于某种原因在尝试使用requests.get()时出现任何互联网连接问题。

I have written the code below but it always prints Oops! Houston, we have a problem!我写了下面的代码,但它总是打印Oops! Houston, we have a problem! Oops! Houston, we have a problem! even if there are no problems when trying to send the request.即使在尝试发送请求时没有问题。

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!")

I would highly appreciate if you could give me some feedback!如果您能给我一些反馈,我将不胜感激! 🙂 🙂

我相信requests.exceptions.RequestException正在捕获所有响应,因为当我从except删除它时它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM