Python requests组件BUG解决:Max retries exceeded with作者:数据无忧 时间:2021-02-03 18:23:24 |
如果大家使用Python来写爬虫的时候,都会使用到requests组件。这个组件是Python调用其他地址最好用的组件之一。 但是今天在Python的web项目中遇到了HTTPConnectionPool(host:XX)Max retries exceeded with url 的BUG。 BUG代码如下: res_data = requests.get(req_url, headers=headers) 下面我们就来说说,该BUG的解决方式: 1.requests组件的版本太落后,需要更新组件 sudo pip install --upgrade requests 2.在requests调用url的过程中,其连接状态是保持着keep-alive持续连接状态的。所以如果频繁调用,系统就会报Max retries exceeded with,这时我们就需要来关闭该连接。 本人直接提供最好的关闭方式。 headers = { 'Connection': 'close', } req_url = 'https://www.baidu.com' s = requests.Session() s.keep_alive = False res_data = None with requests.Session() as s: result_data = s.get(req_url, headers=headers) res_data = ast.literal_eval(result_data.text) print(res_data) 以上就是该问题的解决方式。 无忧代理IP(www.data5u.com)原创文章,转载请注明出处。 |