赞
踩
在访问一个网页时,如果该网页长时间未响应,系统就会判断该网页超时,无法打开网页。模拟代码如下:
import requests
for a in range(0,100):
try:
response = requests.get('http://www.baidu.com/', timeout=0.02)
print(response.status_code)
except Exception as e:
print('异常'+str(e))
抛出的异常:
异常HTTPConnectionPool(host='www.baidu.com', port=80): Read timed out. (read timeout=0.02)
识别网络异常的分类
针对网络异常信息,requests模块同样提供了三种常见的网络异常捕获异常,代码如下:
import requests
from requests.exceptions import ReadTimeout,HTTPError,RequestException
for a in range(0,100):
try:
response = requests.get('http://www.baidu.com/', timeout=0.02)
print(response.status_code)
except ReadTimeout:#超时异常
print('time out')
except HTTPError:#HTTP异常
print('httperror')
except RequestException:#请求异常
print('reqerror')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。