实用爬虫-02-爬虫真正使用代理 ip
# coding:utf-8
# 爬虫使用代理IP
from urllib import request,error
if __name__ == '__main__':
# 0.设置测 ip 的地址
url = "http://2018.ip138.com/ic.asp"
# 1.设置代理 ip,获取方法参照:https://www.cnblogs.com/xpwi/p/9600727.html
proxy = {'http':'189.201.142.129:57815'}
# 2.创建ProxyHandler
proxy_handler = request.ProxyHandler(proxy)
# 3.创建Opener
opener = request.build_opener(proxy_handler)
# 4.安装Opener
request.install_opener(opener)
# 下面再进行访问url就会使用代理服务器
try:
rsp = request.urlopen(url)
html = rsp.read().decode('GBK')
print(html)
except error.HTTPError as e:
print(e)
except Exception as e:
print(e)
运行结果
![1483449-20180908225529621-2075630791.jpg](https://i-blog.csdnimg.cn/blog_migrate/f9ebc94dc98b1d944fb5f8aebf72e1a4.jpeg)