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

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

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

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

多写几个这样的方法

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

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

def 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'9
10     }
11
12     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('可用爬虫ip'+ipprot)
17         if Db.r.llen('Iplist')<=50:
18            Db.add_ip(ipprot)
19         #Alt.iplist.append(ipprot)
20
21         else:
22            Db.add_ips(ipprot)
23     else:
24         print('不可用爬虫ip'+ipprot)

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

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

#检查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

1 #coding:utf-82 import redis3 import Requestdef4 r = redis.Redis(host='127.0.0.1', port=6379)#host后的IP是需要连接的ip,本地是127.0.0.1或者localhost5 #主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 i
16 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 i
24 #取出从最后一个开始
25 def rem_ip():
26     i=str(r.rpop('Iplist'),encoding='utf-8')
27     return i
28 #检查主ip池
29 def act_db():
30     for i in range(int(r.llen('Iplist')/2)):
31        Requestdef.inspect_ip(rem_ip())
32
33 #如果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库创建接口

1 from flask import Flask2 import Db34 app = Flask(__name__)56 @app.route('/', methods=['GET'])7 def home():8     return 'What is?'9
10 @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调用 用一次就删一个。

Python如何爬取免费爬虫ip相关推荐

  1. Python爬虫:爬取免费代理ip

    之前写的几个爬虫都只能爬取到少量的信息,这是由于一个ip频繁地访问网站,会被认定为非正常的爬虫从而被屏蔽,这时候就需要使用代理ip来访问网站了,具体方法就是在发送request时添加一个proxy参数 ...

  2. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个 ...

  3. 多线程爬取免费代理ip池 (给我爬)

    多线程爬取免费代理ip池 (给我爬) 文章目录 多线程爬取免费代理ip池 (给我爬) 安装的库 IP 隐藏 代理ip 多线程爬取 读入代理ip 写入代理ip 验证代理ip 解析网页得到代理ip 获取网 ...

  4. 爬取免费代理IP并测试

    爬取免费代理IP并测试 写在开头:这次总共爬了三个代理ip的网站,前两个网站经过测试,ip并不能访问我真正想爬的网站 Git仓库:https://gitee.com/jiangtongxueya/my ...

  5. golang爬取免费代理IP

    golang爬取免费的代理IP,并验证代理IP是否可用 这里选择爬取西刺的免费代理Ip,并且只爬取了一页,爬取的时候不设置useAgent西刺不会给你数据,西刺也做反爬虫处理了,所以小心你的IP被封掉 ...

  6. 自己动手爬取免费代理IP

    使用爬虫在爬取一些大网站的时候,总会出现被反爬技术阻碍的情况,限制IP就是其中一种. 那么使用代理就是很好的解决方案. 作为一个穷的裤兜比脸干净的人(博主每天洗脸,不要怀疑这一点),花钱去买代理就不在 ...

  7. 简易爬取免费代理IP

    爬取maitian屡次被封,先建立一个免费代理ip池吧 暂时保存为txt格式 思路: 1.找到免费的ip代理网站 以西刺代理的4个网站为例: 国内普通代理: http://www.xicidaili. ...

  8. 爬取免费代理IP代码

    以下代码会抓取西刺代理网站的代理ip: 1.抓取西刺代理网站的代理ip 2.并根据指定的目标url,对抓取到ip的有效性进行验证 3.在和此代码同级文件夹下创建ip.txt文件,将有效的IP存入 # ...

  9. 【Python爬虫案例学习9】python爬取免费优质IP归属地查询接口

    刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就ok了嘛~但是,网上免费接口要么限制访问频率(淘宝的),要么限制访问次数(百度及其他) 没辙了,从百度找到了几个在线查 ...

  10. Python爬虫实战013:Python爬取免费代理ip

    import requests import time import random from lxml import etree from fake_useragent import UserAgen ...

最新文章

  1. TensorFlow MNIST最佳实践
  2. VScode修改快捷键,快捷键恢复默认设置,添加或取消注释
  3. 长脖子鹿省选模拟赛 [LnOI2019SP]快速多项式变换(FPT)
  4. spring 事务管理之只读事务@Transactional(readOnly = true)
  5. mysql日期函数转换_Mysql日期函数大全 转
  6. 关于残差网络(ResNet)解析
  7. java如何开根号?
  8. 一个80后独立开发者
  9. C#番外篇-SpinWait
  10. 【Linux】解决shell脚本中syntax error:unexpected end of file问题
  11. 【LKM】make 错误:make -C /lib/modules/4.15.0-58-generic/build SUBDIRS=/xxx
  12. PS教程证件照底片更换颜色
  13. gitlab安装配置
  14. mysql 3306_允许远程链接mysql,开放3306端口
  15. c++跟踪调试怎么用 dev_DEVC++调试方法
  16. C++Primer笔记-A003-decltype使用
  17. 6、【斯纳克学生微信缴费平台】家长手机微信缴费
  18. excel汇总怎么做?
  19. LayUi做登录注册
  20. 温故而知新--Java基础(三):Java常用集合类(上)

热门文章

  1. 安装windows系统到移动硬盘
  2. c语言函数cot怎么表示,谁知道三角函数sin,cos,tan,cot之间的换算公式?
  3. 【邮箱】Foxmail中如何登陆网易邮箱?
  4. c语言四则混合运算可以带括号,带小括号的四则混合运算听后感
  5. .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  6. 面对换领导或空降管理者,该怎么办?
  7. 芒果魔兽世界单机没法运行服务器,大芒果魔兽世界单机版局域网联机方法
  8. Spark源码系列(二)RDD详解 - 岑玉海
  9. 10年涨7倍!纵观南京房价变迁史,看哭多少买房人!
  10. ps:HSB色彩模式