zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python爬虫报错:Max retries exceeded with url

Python爬虫 报错 with url max exceeded
2023-09-11 14:14:47 时间

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

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://mail.163.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()
url = "https://mail.163.com/"
s.keep_alive = False
s.proxies = {"https": "47.100.104.247:8080", "http": "36.248.10.47:8080", }
s.headers = header
r = s.get(url)
print r.status_code  # 如果代理可用则正常访问,不可用报以上错误