帮助中心 > 新闻资讯 >python爬虫反爬取---设置IP代理自动变换requests.get()中proxy的IP

python爬虫反爬取---设置IP代理自动变换requests.get()中proxy的IP

发布时间:2018-07-04

今天做了个随机变换IP的功能
由于今天懒得写爬虫爬取西刺网 (http://www.xicidaili.com/wt/) 的ip和端口号

西刺网

就简单写了个py来用人肉的方法一个一个试IP(捂脸),事实证明太傻了

__author__ = 'Lee'
from headers import  requests_headers  # 上一篇文章中所写的自动转变headers文件
from bs4 import BeautifulSoup
import requests
header = requests_headers()  #调用requests_headers() 返回一个随机的headers文件
proxies = {'http': 'http://139.0.28.18:8080'}  #这个地方换一下ip和端口号
url = 'http://www.whatismyip.com.tw' #访问这个网站可以返回你的IP地址 以此验证是否变换成功
try:
    wb_data = requests.get(url,headers=header,proxies=proxies,timeout=5) #timeout 限定5秒相应后就退出执行
    soup = BeautifulSoup(wb_data.text,'lxml')
    print(soup)
except(requests.exceptions.ProxyError,requests.exceptions.ConnectTimeout):
    print('failed!')

#国外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

执行后结果

下边是用获得的三个IP做的自动变换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())

相关推荐