|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 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: [WinError 10054] 远程主机强迫关闭了一个现有的连接。
代理的IP试了几个,还是一样,是IP地址不行吗?还是写错了?
|
|