python 55 访问whatismyip 有问题
本帖最后由 checkily 于 2018-3-8 11:51 编辑代码如下:
import urllib.request
url = 'http://www.whatismyip.com.tw'
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
可以正常访问到数据
但加入代理后:
import urllib.request
url = 'http://www.whatismyip.com.tw'
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
# 加入代理
proxy_support = urllib.request.ProxyHandler({'http':'113.86.221.157:61234'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
会提示:ConnectionResetError: 远程主机强迫关闭了一个现有的连接。
代理的IP试了几个,还是一样,是IP地址不行吗?还是写错了?
本帖最后由 checkily 于 2018-3-8 11:51 编辑
占个楼 是代理ip的问题,你可以试试这个: proxy_support = urllib.request.ProxyHandler({'http' : '118.31.220.3:8080' })
页:
[1]