python ip代理
1 import random 2 import urllib.request 3 from bs4 import BeautifulSoup 4 import time 5 6 url ='http://www.whatismyip.com.tw/' 7 for i in range(0,5): 8 9 iplist = ['111.13.7.123:80','117.143.109.152:80','59.127.154.78:80'] 10 proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)}) 11 opener=urllib.request.build_opener(proxy_support) 12 opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36')] 13 urllib.request.install_opener(opener) 14 15 16 17 18 response = urllib.request.urlopen(url) 19 20 html = response.read().decode('utf-8') 21 22 soup=BeautifulSoup(html,'html.parser') 23 24 a=soup.find_all("script",{"type":"application/json"}) 25 26 print(a) 27 time.sleep(1)
注意的是要验证代理ip是否可用
大家可以参考这个网站
http://www.66ip.cn/yz/