代码改变世界

爬虫替换ip

2017-06-14 17:15  hl_流星  阅读(591)  评论(0编辑  收藏  举报
import urllib.request
import random

ip=["122.72.56.112:8080","118.144.67.50:8118"]
url1 = "http://www.whatismyip.com.tw"
proxy_support = urllib.request.ProxyHandler({"http":random.choice(ip)})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders=[("User-Agent",'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')]
urllib.request.install_opener(opener)
response=opener.open(url1)
url = response.read().decode("utf-8")
print(url)