python爬虫 关于Max retries exceeded with url 的错误

爬取安逸花 https://vayh.msxf.com/ 时出现这种错误,总结如下:

1.https连接太多没有关闭导致的433,解决方法:

import requests
requests.adapters.DEFAULT_RETRIES = 5 # 增加重连次数
s = requests.session()
s.keep_alive = False # 关闭多余连接
s.get(url) # 你需要的网址

2.访问次数频繁,被禁止访问,解决方法:使用代理

import requests
s = requests.session()
url = "https://vayh.msxf.com/"
s.proxies = {"https": "47.100.104.247:8080", "http": "36.248.10.47:8080", }
s.headers = header
s.get(url)

查找代理的网址:http://ip.zdaye.com/shanghai_ip.html#Free
使用代理时需注意:
1.代理分为http和https两种,不能用混,如果把http的代理用作https也是会报上面的错误;
2.上面的代理以字典格式传入,例如上面的例子,可以是“47.100.104.247:8080”这种格式,也可以是“https://47.100.104.247:8080”这种格式
3.如果代理不可用一样会报上面的错误。
以下方法判断代理是否可用:

import requests
s = requests.session()
s.keep_alive = False
url = "https://vayh.msxf.com/loan/replacevToken?"
headers = {
            'Host': 'vayh.msxf.com',
            'X-Requested-With': 'XMLHttpRequest',
            'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1301.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat",
            'Cookie': cookie,
         }
params = {
    'mobile': phone,
    'password': '140e0d2deeb6d6b1b803087c03821448c95f3be61ffd27c89f6c391a3288a838',
    'saMobile': '',
    'smsCode': '',
    'channel': 'MS_WeChat_AYH',
}
url += urlencode(params)
response = s.get(url, headers=headers, proxies=proxies, verify=False,timeout=10)
print(response.status_code)

如果代理可用则正常访问,返回code==200说明问题已解决

原文地址:https://www.cnblogs.com/gqv2009/p/13215182.html