热线电话:13121318867

登录
2019-04-08 阅读量: 908
python爬虫异常

问题描述:

python爬虫异常报错:requests.exceptions.ConnectionError

解决方法:

对于这个错误,stackoverflow给出的解释是

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a ConnectionError exception.

翻译过来就是说这是网络问题出现的异常事件(如DNS错误,拒绝连接,等等),这是Requests库中自带的异常

一种解决办法是捕捉基类异常,这种方法可以处理所有的异常情况:

try:
r = requests.get(url, params={’s’: thing})
except requests.exceptions.RequestException as e: # This is the correct syntax
print e
sys.exit(1)

另外一种解决办法是分别处理各种异常,这里面有三种异常:

try:
r = requests.get(url, params={’s’: thing})
except requests.exceptions.Timeout:
except requests.exceptions.TooManyRedirects:
except requests.exceptions.RequestException as e:
print e
sys.exit(1)
0.0000
6
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子