问题:
在使用代理服务器访问 http://httpbin.org/ip,测试返回IP地址,原python代码:
import requests
url='http://httpbin.org/ip'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
}
proxy={
'http':'http://125.74.93.60:8083'
# 'http':'http://202.102.86.228:8080',
# 'http':'http://111.43.79.98:8085',
# 'http':'http://39.103.175.42:3128',
# "http":"http://127.0.0.1:80",
}
response=requests.get(url=url,headers=headers,proxies=proxy)
print(response.text)
看来许多博主request的使用方法,也没问题啊,但是运行起来,报错如下:
requests.exceptions.ProxyError: HTTPConnectionPool(host='125.74.93.60', port=8083): Max retries exceeded with url: http://httpbin.org/ip (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000013231F13088>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
解决办法:
headers中加上'Connection' key,值为‘close',就可以了。
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36',
'Connection':'close'}