import random
import urllib.request
from bs4 import BeautifulSoup
import time url ='http://www.whatismyip.com.tw/'
for i in range(0,5): iplist = ['111.13.7.123:80','117.143.109.152:80','59.127.154.78:80']
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 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36')]
urllib.request.install_opener(opener) response = urllib.request.urlopen(url) html = response.read().decode('utf-8') soup=BeautifulSoup(html,'html.parser') a=soup.find_all("script",{"type":"application/json"}) print(a)
time.sleep(1)
注意的是要验证代理ip是否可用
大家可以参考这个网站
http://www.66ip.cn/yz/