zoukankan      html  css  js  c++  java
  • 反爬虫之搭建IP代理池

    反爬虫之搭建IP代理池

    听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部。可惜加了header请求头,加了cookie 还是被限制爬取了。这时就得祭出IP代理池!!!
    enter description here

    下面就是requests使用ip代理例子

    response = requests.get(url,proxies=proxies)
    

    这样就可以使用你定义的代理地址去访问网站了

    但IP代理哪里来阿?有两种方式

    1. 付费API接口(IP量多,稳定)
    2. 免费IP代理 (IP量少,不稳定,但免费呀,基础爬虫已够)

    enter description here

    这时我们可以搞个IP代理池,思路就是通过python爬取大量免费代理IP,然后进行存活验证,再提供接口使用。

    其实这种事早就有人写了,github上有很多优秀的项目,这里分享两个。(问题是我不会写,Python从入门到放弃)

    1. https://github.com/jhao104/proxy_pool
    2. https://github.com/Python3WebSpider/ProxyPool

    这里我用第一个项目吧,毕竟Star 6k多

    这时先安装一下redis(步骤省略,记得勾上添加环境变量和放行防火墙)
    enter description here

    以上说明我们已经成功安装了redis

    下面我们将redis安装成windows服务,自启动

    进到redis目录下,然后执行redis-server --service-install redis.windows-service.conf
    enter description here

    注意:不要找错了 通常为redis.windows-service.conf 而不是redis.windows.conf 后者是以非系统服务方式启动程序使用的配置文件。

    这时还得去看服务有没有开启
    enter description here

    修改数据库密码等配置项,一般测试环境都无需加密码,这里还是加一下吧

    打开Redis服务配置文件redis.windows-service.conf,找到含有requirepass字样的地方,追加一行,输入requirepass 12345 并保存!!!
    enter description here

    重启一下服务(刚才是开着的,现在先关后开)
    enter description here

    Tips!!!
    启动临时服务: redis-server.exe redis.windows.conf
    客户端调用: redis-cli.exe -h 127.0.0.1 -p 6379
    卸载服务: redis-server.exe --service-uninstall

    打开RedisClient 管理工具 点击 服务器 > 添加
    enter description here

    配置proxy_pool项目,首先在项目目录下安装依赖
    enter description here

    配置Config/setting.py 其中如果有更改IP、端口、密码的在圈圈里面修改并保存!(代理池访问网址端口也可以在文件最底下改)
    enter description here

    上面配置启动后,默认代理池访问地址为 http://127.0.0.1:5010

    如果你的依赖已经安全完成并且具备运行条件,可以直接在Run下运行main.py。

    如果运行成功你应该看到有4个main.py进程,你也可以分别运行他们,依次到Api下启动ProxyApi.py,Schedule下启动ProxyRefreshSchedule.py和ProxyValidSchedule.py即可。

    把项目跑起来!!!
    enter description here

    在RedisClient 工具下查看数据
    enter description here

    API使用
    enter description here

    大致搭建过程就在这里啦,不过免费的IP代理还是差很多,生产环境需求最好还是花钱购买API接口,质量好!!!

    作者:YinJay

  • 相关阅读:
    lear for video
    My needs test
    jungle
    因文章很荣幸的被别人抄袭了,为了刚这种人,决定以后将文章都用英文发布出来
    atom 安装multi-cursor 插件 实现多行编辑
    Typora下使用markdown进行插入图片
    dockerhub/jenkins
    promethus grafana dingtalk pushgateway alertermanager
    关于自律比较好的一篇文章
    @WebService这个标签的作用是什么
  • 原文地址:https://www.cnblogs.com/YinJay/p/10909442.html
Copyright © 2011-2022 走看看