今天做了個(gè)隨機(jī)變換IP的功能
由于今天懶得寫(xiě)爬蟲(chóng)爬取西刺網(wǎng) (http://www.xicidaili.com/wt/) 的ip和端口號(hào)
西刺網(wǎng)
就簡(jiǎn)單寫(xiě)了個(gè)py來(lái)用人肉的方法一個(gè)一個(gè)試IP(捂臉),事實(shí)證明太傻了
__author__ = 'Lee'
from headers import requests_headers # 上一篇文章中所寫(xiě)的自動(dòng)轉(zhuǎn)變headers文件
from bs4 import BeautifulSoup
import requests
header = requests_headers() #調(diào)用requests_headers() 返回一個(gè)隨機(jī)的headers文件
proxies = {'http': 'http://139.0.28.18:8080'} #這個(gè)地方換一下ip和端口號(hào)
url = 'http://www.whatismyip.com.tw' #訪問(wèn)這個(gè)網(wǎng)站可以返回你的IP地址 以此驗(yàn)證是否變換成功
try:
wb_data = requests.get(url,headers=header,proxies=proxies,timeout=5) #timeout 限定5秒相應(yīng)后就退出執(zhí)行
soup = BeautifulSoup(wb_data.text,'lxml')
print(soup)
except(requests.exceptions.ProxyError,requests.exceptions.ConnectTimeout):
print('failed!')
#國(guó)外IP 1.179.183.86:8080 113.53.231.201:3129 182.23.28.180:3128 182.253.177.59:3128 139.0.28.18:8080
執(zhí)行后結(jié)果
下邊是用獲得的三個(gè)IP做的自動(dòng)變換proxy文件
__author__ = 'Lee'
import random
ip_pool = [
'119.98.44.192:8118',
'111.198.219.151:8118',
'101.86.86.101:8118',
]
def ip_proxy():
ip = ip_pool[random.randrange(0,3)]
proxy_ip = 'http://'+ip
proxies = {'http':proxy_ip}
return proxies
print(ip_proxy())