代码之家  ›  专栏  ›  技术社区  ›  Anatol

蟒蛇请求。获取():如果出现Internet连接错误,如何打印消息?

  •  1
  • Anatol  · 技术社区  · 5 年前

    我正在尝试打印错误消息: Oops! Houston, we have a problem! requests.get() .

    我已经写了下面的代码,但它总是打印 哎呀!休斯顿,我们有麻烦了!

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

    1 回复  |  直到 5 年前
        1
  •  1
  •   clubby789 uppi    5 年前

    我相信 requests.exceptions.RequestException 正在捕获所有的响应,就像我从 except