Python请求异常处理 [英] Python requests exception handling

查看:180
本文介绍了Python请求异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python库请求处理异常?
例如如何检查PC连接到互联网?

How to handle exceptions with python library requests? For example how to check is PC connected to internet?

当我尝试

try:
    requests.get('http://www.google.com')
except ConnectionError:
    # handle the exception

它给我错误名称 ConnectionError 未定义

推荐答案

假设你做了导入请求,你需要 requests.ConnectionError ConnectionError 是由请求定义的异常。请参阅 API文档

Assuming you did import requests, you want requests.ConnectionError. ConnectionError is an exception defined by requests. See the API documentation here.

这篇关于Python请求异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆