requests 重试
话不多说,贴代码
import requests
from retrying import retry
headers = {}
@retry(stop_max_attempt_number=3) # 最大重试3次,3次全部报错,才会报错
def _parse_url(url):
print(url)
response = requests.get(url, headers=headers, timeout=3) # 超时的时候回报错并重试
assert response.status_code == 200 # 状态码不是200,也会报错并充实
return response
try: # 进行异常捕获
response = _parse_url("127.0.0.1")
except Exception as e:
print(e)
response = None