Python爬虫报错:Max retries exceeded with url

阅读: 评论:0

Python爬虫报错:Max retries exceeded with url

Python爬虫报错:Max retries exceeded with url

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 = "/"
s.proxies = {"https": "47.100.104.247:8080", "http": "36.248.10.47:8080", }
s.headers = header
s.get(url)

查找代理的网址:

.html#Free

使用代理时需注意:

  1. 代理分为http和https两种,不能用混,如果把http的代理用作https也是会报上面的错误;
  2. 上面的代理以字典格式传入,例如上面的例子,可以是“47.100.104.247:8080”这种格式,也可以是“47.100.104.247:8080”这种格式;
  3. 如果代理不可用一样会报上面的错误。

以下方法判断代理是否可用:

import requests
s = requests.session()
url = "/"
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  # 如果代理可用则正常访问,不可用报以上错误

本文发布于:2024-01-29 10:23:41,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170649502614617.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:爬虫   报错   Python   Max   url
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23