使用ip付费的代理是遇到http.client.RemoteDisconnected: Remote end closed connection without response 解决方法
上图是我们使用代理是的报错信息,在网上搜索了一下,答案都是这个问题不需要的。这里我用的是http协议,ip是从X宝买的。
请求的url也是http协议,下图是我测试ip的百度网站。这个位置犯了错误,https://www.baidu.com/ 百度真确写法。
当然这个位置测试网站换成https://blog.****.net/lovlu18 协议时,它还是可以得到响应,这个因为没有ssl加密应用层的介入
所有还是建议大家使用http协议的url。
那就贴上代码,请求的url是内涵8 http://www.neihan8.com/article/list_5_1.html
import urllib.request # 调用ProxyHandler 代理IP的形式是字典 # 付费的代理 # money_proxy = {'协议':'username:[email protected]:port'} px = urllib.request.ProxyHandler({'http': 'tt412:[email protected]:888'}) # 用build_opener()来构建一个opener对象 opener = urllib.request.build_opener(px) # 然后调用构建好的opener对象里面的open方法来发生请求。 # 实际上urlopen也是类似这样使用内部定义好的opener.open(),这里就相当于我们自己重写。 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"} res = urllib.request.Request('http://www.neihan8.com/article/list_5_1.html', headers=headers) response = opener.open(res, timeout=3) print(response) re = response.read().decode('gbk') print(re)