前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >反爬虫之搭建IP代理池

反爬虫之搭建IP代理池

作者头像
py3study
发布2020-01-16 16:15:58
2.4K0
发布2020-01-16 16:15:58
举报
文章被收录于专栏:python3python3

反爬虫之搭建IP代理池

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

enter description here
enter description here

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

代码语言:javascript
复制
response = requests.get(url,proxies=proxies)

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

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

  1. 付费API接口(IP量多,稳定)
  2. 免费IP代理 (IP量少,不稳定,但免费呀,基础爬虫已够)
enter description here
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
enter description here

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

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

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

enter description here
enter description here

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

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

enter description here
enter description here

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

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

enter description here
enter description here

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

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
enter description here

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

enter description here
enter description here

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

enter description here
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
enter description here

在RedisClient 工具下查看数据

enter description here
enter description here

API使用

enter description here
enter description here

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 反爬虫之搭建IP代理池
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档