微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python如何爬取免费爬虫ip

做过大数据抓取的程序员应该都知道,正常市面上的爬虫ip只分为两种,一种是API提取式的,还有一种是账密形式隧道模式的。往往因为高昂费用而止步。对于初学者觉得没有必要,我们知道每个卖爬虫ip的网站有的提供了免费IP,可是又很少,写了个爬虫ip池 。学习应该就够了。

爬虫ip池:
1,在各大网站爬取免费爬虫ip
2,检查ip可用 可用存入数据库1和2
3,在数据库1中拿出少量爬虫ip存入数据库2(方便维护)
4,定时检查数据库1和数据库2的爬虫ip数量,以及是否可用
5,调用端口

1、各大网站爬取免费爬虫ip

 @H_404_17@1 def IPList_61():
 @H_404_17@2   for q in [@H_404_17@1,@H_404_17@2]:
 @H_404_17@3       url='http://jshk.com.cn/'+str(q)+'.html'
 @H_404_17@4       html=Requestdef.get_page(url)
 @H_404_17@5       if html!=None:
 @H_404_17@6           #print(html)
 @H_404_17@7           iplist=BeautifulSoup(html,'lxml')
 @H_404_17@8           iplist=iplist.find_all('tr')
 @H_404_17@9           i=@H_404_17@2
@H_404_17@10           for ip in iplist:
@H_404_17@11              if i<=@H_404_17@0:
@H_404_17@12                  loader=''
@H_404_17@13                  #print(ip)
@H_404_17@14                  j=@H_404_17@0
@H_404_17@15                  for ipport in ip.find_all('td',limit=@H_404_17@2):
@H_404_17@16                      if j==@H_404_17@0:
@H_404_17@17                         loader+=ipport.text.strip()+':'
@H_404_17@18                      else:
@H_404_17@19                          loader+=ipport.text.strip()
@H_404_17@20                      j=j+@H_404_17@1
@H_404_17@21                  Requestdef.inspect_ip(loader)
@H_404_17@22              i=i-@H_404_17@1
@H_404_17@23       time.sleep(@H_404_17@1)

多写几个这样的方法

2,检查ip可用 可用存入数据库1,和2。

3,在数据库1中拿出少量爬虫ip存入数据库2(方便维护)。

def inspect_ip(ipprot):
 @H_404_17@2     time.sleep(@H_404_17@1)
 @H_404_17@3     herder={
 @H_404_17@4         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/62.0.3202.94 Safari/537.36",
 @H_404_17@5         'Accept-Encoding':'gzip,deflate',
 @H_404_17@6         'Accept-Language':'zh-CN,zh;q=0.9',
 @H_404_17@7         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
 @H_404_17@8         'Upgrade-Insecure-Requests':'1'
 @H_404_17@9
@H_404_17@10     }
@H_404_17@11
@H_404_17@12     url='https://www.baidu.com'
@H_404_17@13     proxies = { "http": "http://"+str(ipprot) }
@H_404_17@14     request=requests.get(url,headers=herder,proxies=proxies)
@H_404_17@15     if request.status_code==@H_404_17@200:
@H_404_17@16         print('可用爬虫ip'+ipprot)
@H_404_17@17         if Db.r.llen('Iplist')<=@H_404_17@50:
@H_404_17@18            Db.add_ip(ipprot)
@H_404_17@19         #Alt.iplist.append(ipprot)
@H_404_17@20
@H_404_17@21         else:
@H_404_17@22            Db.add_ips(ipprot)
@H_404_17@23     else:
@H_404_17@24         print('不可用爬虫ip'+ipprot)

我这里是用的www.baidu.com检测的 给主IP的数据库长度是50 (太多了不好维护)。

4、定时检查数据库1和数据库2的爬虫ip数量,以及是否可用

#检查ip池数量
def time_ip():
 
    while True:
        time.sleep(@H_404_17@5)
        Db.act_lenip()
 
#检查备用池数量
def time_ips():
    while True:
        time.sleep(@H_404_17@30)<br>        #当备用池数量小于100 再次获取
        if Db.len_ips()<@H_404_17@100:
            print('填数据')
            Acting_ip.iplist()
#程序入口
if __name__ == '__main__':
 
  t1=threading.Thread(target=time_ip)
  t1.start()
  t2=threading.Thread(target=time_ips)
  t2.start()
  t1.join()
  t2.join()

给他2个线程
Db.py

@H_404_17@1 #coding:utf-8
 @H_404_17@2 import redis
 @H_404_17@3 import Requestdef
 @H_404_17@4 r = redis.Redis(host='127.0.0.1', port=@H_404_17@6379)#host后的IP是需要连接的ip,本地是127.0.0.1或者localhost
 @H_404_17@5 #主ip池
 @H_404_17@6 def add_ip(ip):
 @H_404_17@7      r.lpush('Iplist',ip)
 @H_404_17@8 #备用ip池
 @H_404_17@9 def add_ips(ip):
@H_404_17@10      r.lpush('Iplists',ip)
@H_404_17@11 #备用ip池第一个开始取出
@H_404_17@12 def app_ips():
@H_404_17@13      i=str(r.lindex('Iplists',@H_404_17@1),encoding='utf-8')
@H_404_17@14      r.lrem('Iplists',i,num=@H_404_17@0)
@H_404_17@15      return i
@H_404_17@16 def len_ips():
@H_404_17@17     return r.llen('Iplists')
@H_404_17@18 def len_ip():
@H_404_17@19     return r.llen('Iplist')
@H_404_17@20 #第一个开始取出
@H_404_17@21 def app_ip():
@H_404_17@22      i=str(r.lpop('Iplist'),encoding='utf-8')
@H_404_17@23      return i
@H_404_17@24 #取出从最后一个开始
@H_404_17@25 def rem_ip():
@H_404_17@26     i=str(r.rpop('Iplist'),encoding='utf-8')
@H_404_17@27     return i
@H_404_17@28 #检查主ip池
@H_404_17@29 def act_db():
@H_404_17@30     for i in range(int(r.llen('Iplist')/@H_404_17@2)):
@H_404_17@31        Requestdef.inspect_ip(rem_ip())
@H_404_17@32
@H_404_17@33 #如果ip池数量少于25个 则填满
@H_404_17@34 def act_lenip():
@H_404_17@35     if r.llen('Iplist')<@H_404_17@25:
@H_404_17@36         print('填ip')
@H_404_17@37         while r.llen('Iplist')<=@H_404_17@50:
@H_404_17@38           Requestdef.inspect_ip(app_ips())

5、调用端口 使用flask库创建接口

@H_404_17@1 from flask import Flask
 @H_404_17@2 import Db
 @H_404_17@3
 @H_404_17@4 app = Flask(__name__)
 @H_404_17@5
 @H_404_17@6 @app.route('/', methods=['GET'])
 @H_404_17@7 def home():
 @H_404_17@8     return 'What is?'
 @H_404_17@9
@H_404_17@10 @app.route('/get', methods=['GET'])
@H_404_17@11 def homsse():
@H_404_17@12     return Db.app_ip()
@H_404_17@13 #线程池数量
@H_404_17@14 @app.route('/count', methods=['GET'])
@H_404_17@15 def homsssse():
@H_404_17@16     return str(Db.len_ip())
@H_404_17@17 app.run(debug=True)

就完成了

在这里插入图片描述


在这里插入图片描述

运行api

在这里插入图片描述

数据库里面的 Iplist为主Ip池 iplist 为备用ip池。

用get调用 用一次就删一个

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐