簡體   English   中英

Python requests.get():如果出現 Internet 連接錯誤,如何打印消息?

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

我正在嘗試打印錯誤消息: Oops! Houston, we have a problem! Oops! Houston, we have a problem! 如果由於某種原因在嘗試使用requests.get()時出現任何互聯網連接問題。

我寫了下面的代碼,但它總是打印Oops! Houston, we have a problem! 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!")

如果您能給我一些反饋,我將不勝感激! 🙂

我相信requests.exceptions.RequestException正在捕獲所有響應,因為當我從except刪除它時它起作用了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM