python 爬虫 代理1

import random
import urllib.request
ippools=[
"39.104.50.71:8080",
"39.104.50.160:8080",
"39.104.14.202:8080",
"39.104.50.137:8080",
"39.104.50.29:8080"
]
def ip(ippools):
thisip=random.choice(ippools)
print(thisip)
proxy=urllib.request.ProxyHandler({"http": thisip})
opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
urllib.request.install_opener(opener)

for i in range(0,5):
try:
ip(ippools)
url="http://www.baidu.com"
data=urllib.request.urlopen(url).read()
print(len(data.decode()))
except Exception as err:
print(err)
posted @ 2017-11-22 22:20  沧海一粒水  阅读(115)  评论(0编辑  收藏  举报