python基础学习1-网络爬虫程序中的代理IP设置

#!/usr/bin/env python
# -*- coding:utf-8 -*-网络爬虫代理
import urllib.request
import random
url="http://www.whatismyip.com.tw"
#使用单个IP
proxy_support = urllib.request.ProxyHandler({'http':'218.249.198.30:3128'})
#使用个IP 列表
iplist=['114.113.220.99:99999','218.249.198.30:3128','59.44.152.110:9999','58.247.125.205:80','122.72.18.160:80']
#随机从IP列表中选择一个IP
#proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders=[("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36")]
urllib.request.install_opener(opener)
re= urllib.request.urlopen(url)
html=re.read().decode('utf-8')
print(html)

 

posted @ 2016-08-30 17:46  whzym111  阅读(194)  评论(0编辑  收藏  举报