python - 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!")
如果您能给我一些反馈,我将不胜感激!
解决方案
I believe requests.exceptions.RequestException
is catching all the responses, as it worked when I removed this from the except
推荐阅读
- sql - Does the SQL CASE statement treat variables differently from columns?
- python - 为什么我不能在我的 Matplotlib/Cartopy 地图上添加图像?
- vba - 循环数据以填充表单并导出到单个 PDF
- ruby-on-rails - has_many :通过不加载记录
- angular - 在 ng 中使用功能模块的推荐做法是什么?
- javascript - 决定应用程序的 Javascript 发布/订阅模式
- python - 没有名为 flask_bcrypt 的模块
- spring-boot - Spring Boot - Hikari 数据库配置
- java - AWS 上的 Apache 目录工作室
- php - PHP如何使用数据库数据制作下拉菜单