我何必说谎 发表于 2019-9-6 15:39:08

Python爬虫之ip代理池


   可能在学习爬虫的时候,遇到很多的反爬的手段,封ip 就是其中之一。

    对于封IP的网站。需要很多的代理IP,去买代理IP,对于初学者觉得没有必要,每个卖代理IP的网站有的提供了免费IP,可是又很少,写了个IP代理池 。学习应该就够了

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

1,在各大网站爬去免费代理ip
def IPList_61():
for q in :
      url='http://www.66ip.cn/'+str(q)+'.html'
      html=Requestdef.get_page(url)
      if html!=None:
          #print(html)
          iplist=BeautifulSoup(html,'lxml')
          iplist=iplist.find_all('tr')
          i=2
          for ip in iplist:
             if i<=0:
               loader=''
               #print(ip)
               j=0
               for ipport in ip.find_all('td',limit=2):
                     if j==0:
                        loader+=ipport.text.strip()+':'
                     else:
                         loader+=ipport.text.strip()
                     j=j+1
               Requestdef.inspect_ip(loader)
             i=i-1
      time.sleep(1)

多写几个这样的方法


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

3,在数据库1中拿出少量代理ip存入数据库2(方便维护)
?123456789101112131415161718192021222324def inspect_ip(ipprot): 2   time.sleep(1) 3   herder={ 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", 5         'Accept-Encoding':'gzip, deflate', 6         'Accept-Language':'zh-CN,zh;q=0.9', 7         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 8         'Upgrade-Insecure-Requests':'1' 910   }1112   url='https://www.baidu.com'13   proxies = { "http": "http://"+str(ipprot) }14   request=requests.get(url,headers=herder,proxies=proxies)15   if request.status_code==200:16         print('可用代理'+ipprot)17         if Db.r.llen('Iplist')<=50:18            Db.add_ip(ipprot)19         #Alt.iplist.append(ipprot)2021         else:22            Db.add_ips(ipprot)23   else:24         print('不可用代理'+ipprot)
我这里是用的www.baidu.com检测的 给主IP的数据库长度是50 (太多了不好维护)

4,定时检查数据库1和数据库2的代理数量,以及是否可用
#检查ip池数量
def time_ip():

    while True:
      time.sleep(5)
      Db.act_lenip()

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


5,调用端口 使用flask库创建接口
?12345678910111213141516171 from flask import Flask 2 import Db 3 4 app = Flask(__name__) 5 6 @app.route('/', methods=['GET']) 7 def home(): 8   return 'What is?' 910 @app.route('/get', methods=['GET'])11 def homsse():12   return Db.app_ip()13 #线程池数量14 @app.route('/count', methods=['GET'])15 def homsssse():16   return str(Db.len_ip())17 app.run(debug=True)
就完成了

运行api



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


用get调用 用一次就删一个


小白代码


ximenyan 发表于 2020-1-2 20:23:15

great,赞一个

ximenyan 发表于 2020-1-2 20:23:57

有才人,:loveliness:
页: [1]
查看完整版本: Python爬虫之ip代理池