python requests 报错 Connection aborted ConnectionResetError RemoteDisconnected 解决方法

错误:requests.exceptions.ConnectionError: (‘Connection aborted.’, RemoteDisconnected(‘Remote end closed connection without response’,))

解决方法

(1)随机切换User-Agent:

 user_agent_list = ["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/61.0",
                    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                    "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
                    ]
                headers['User-Agent'] = random.choice(user_agent_list)

(2)访问频率的设置:
time.sleep(6) #测试一下,如果是与访问频率有关可设置时间长一点
(3)使用代理ip:

self.proxies = {
            "http": ip,
            "https":ip,
        }

错误: urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))

一个循环内,执行request大约240次后,就报错误,原因是服务器把这个当成了攻击

解决方法:

头部需要引入

import time

在调用request的循环内,加入方法

time.sleep(0.5)

幸好数据量不大,0.5秒的间隔尚且忍受。问题解决

 

 

最新回复(0)
/jishuDOBnOHbTs0grofqVm_2FVLd1661TfaSt5fjfLqE8wkWso_3D4674479
8 简首页