html = urllib.request.urlopen("http://baidu.com")
# 报错信息:Traceback (most recent call last): ....... ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
# 主要原因是采用了网络代理
# 解决如果用的urlopen
proxy_handler = urllib.request.ProxyHandler({
'http':'http://XXX'
})
opener = urllib.request.build_opener(proxy_handler)
head={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36","Accept-Language": "zh-CN,zh;q=0.9"}
for key,value in head.items():
headers.append((key,value))
opener.addheads = headers # 添加UA
urllib.request.install_opener(opener)
urllib.request.urlopen("http://baidu.com")
# 直接采用requests
proxies = {
'http':'http://XXX'
}
requests.get("http://baidu.com",headers=head,verify=False,proxies=proxies)
Python错误:ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
最新推荐文章于 2024-05-28 11:01:49 发布