免费代理池的搭建
参考资料:Python3 网络爬虫开发实战(第二版)
一. 学习目标 :
参考《第9章 代理的使用》9.2节搭建自己私有的代理IP池。
二. 实验环境:
阿里云
或腾讯云
的服务器,我是在打折和新用户优惠的时候入手的,比较便宜,均是入门级配置的服务器,阿里云99元一年,1核2G,100G硬盘,腾讯云222元3年,2核4G,80G硬盘,但每月有1200G流量限制。阿里云的系统镜像为Centos7.9,腾讯云的系统镜像为Centos7.6
三. 免费代理池搭建
3.1 步骤说明:
- 购买阿里云或腾讯云服务器,选择Centos7系统镜像。
- 使用
SSH工具
远程登入到你购买的服务器。 - 安装
Anaconda Python环境
:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.05-Linux-x86_64.sh - 安装
Redis数据库
:http://download.redis.io/releases/redis-3.0.5.tar.gz - 下载崔老师的
ProxyPool项目
代码:https://github.com/Python3WebSpider/ProxyPool - 参考ProxyPool项目的
README.md
运行项目
前两步就省略了,如果有问题可以留言。
3.2 安装Anaconda Python环境
参考链接:https://zhuanlan.zhihu.com/p/64930395,下面就只列具体操作,关于命令的解释可完全参考该网址中的内容。
3.2.1 下载Anaconda
直接在官网下载挺慢的,建议使用清华镜像(可以把pip的源也换成国内的,pip install也会快很多):Tsinghua Open Source Mirror
[root@VM-4-17-centos ~]# wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2021.05-Linux-x86_64.sh
3.2.2 安装Anaconda
[root@VM-4-17-centos ~]# bash Anaconda3-2021.05-Linux-x86_64.sh
一路按回车,遇到 license 就输入yes
Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':
>>> yes
之后会指定目录安装,我选择的是默认路径安装,直接按回车即可
Anaconda3 will now be installed into this location:
/root/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/root/anaconda3] >>>
提示是否要初始化Anaconda环境,我选择的是
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>>
看到如下提示则安装成功:
Thank you for installing Anaconda3!
===========================================================================
Anaconda and JetBrains are working together to bring you Anaconda-powered
environments tightly integrated in the PyCharm IDE.
PyCharm for Anaconda is available at:
https://www.anaconda.com/pycharm
3.2.3 配置环境变量
打开profile文件
vi /etc/profile
在文件最后加入如下语句(路径需要根据自己的安装位置更改):
PATH=$PATH:/root/anaconda3/bin
export PATH
3.2.4 重启服务器
重启后看到命令行前面有(base)证明Anaconda安装成功
(base) [root@VM-4-17-centos ~]#
3.3 安装redis
参考链接:https://www.cnblogs.com/zuidongfeng/p/8032505.html
3.3.1 下载源码&解压
(base) [root@VM-4-17-centos ~]# wget http://download.redis.io/releases/redis-3.0.5.tar.gz
3.3.2 编译&安装
(base) [root@VM-4-17-centos ~]# tar xvf redis-3.0.5.tar.gz
(base) [root@VM-4-17-centos ~]# cd redis-3.0.5/
(base) [root@VM-4-17-centos redis-3.0.5]# make MALLOC=libc
(base) [root@VM-4-17-centos redis-3.0.5]# cd src && make install
3.3.3 启动redis服务
刚安装完,启动下试试
(base) [root@VM-4-17-centos src]# ./redis-server
13193:C 20 Mar 09:16:59.524 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 13193
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
这是在前台启动,不方便,ctrl+c
终止,换成后台运行
# 设置为后台运行
(base) [root@VM-4-17-centos src]# cd ..
(base) [root@VM-4-17-centos redis-3.0.5]# vi redis.conf
# 将 daemonize no 修改为 daemonize yes
(base) [root@VM-4-17-centos redis-3.0.5]# ./src/redis-server ./redis.conf
# 查看 redis 进程是否启动
(base) [root@VM-4-17-centos redis-3.0.5]# ps aux | grep redis
root 13790 0.0 0.0 131396 1864 ? Ssl 09:21 0:00 ./src/redis-server *:6379
root 13807 0.0 0.0 112812 980 pts/2 S+ 09:21 0:00 grep --color=auto redis
# 关闭 13790 进程
(base) [root@VM-4-17-centos redis-3.0.5]# kill 13790
这是手动开启redis服务,依然不方便,设置为开机自启服务
# 设置开机自启
(base) [root@VM-4-17-centos redis-3.0.5]# mkdir /etc/redis
(base) [root@VM-4-17-centos redis-3.0.5]# cp ./redis.conf /etc/redis/6379.conf
(base) [root@VM-4-17-centos redis-3.0.5]# cp ./utils/redis_init_script /etc/init.d/redisd
(base) [root@VM-4-17-centos redis-3.0.5]# vi /etc/init.d/redisd
# 头部加入两行,意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
(base) [root@VM-4-17-centos redis-3.0.5]# service redisd start
Starting Redis server...
(base) [root@VM-4-17-centos redis-3.0.5]# ps aux | grep redis
root 15326 0.0 0.0 27700 1876 ? Ssl 09:31 0:00 /usr/local/bin/redis-server *:6379
root 15330 0.0 0.0 112812 976 pts/2 S+ 09:31 0:00 grep --color=auto redis
只做到这还不行,还得参考 https://blog.csdn.net/qq_40606798/article/details/82286273 链接中的方法三
# 修改 /etc/rc.d/rc.local 这个文件,末尾添加 service redisd start
(base) [root@VM-4-17-centos redis-3.0.5]# vi /etc/rc.d/rc.local
# 保存退出,确保/etc/rc.d/rc.local文件有执行权限
# 重启服务器,检查 redis 是否自动开启
**很重要:**这里建议把 /etc/rc.d/rc.local 备份一下,因为后面的步骤可能会清空这个文件,执行下面命令
(base) [root@VM-4-17-centos redis-3.0.5]# cp /etc/rc.d/rc.local /etc/rc.d/rc.local.bak
3.4 部署ProxyPool项目
3.4.1 下载ProxyPool项目
源码
git clone https://github.com/Python3WebSpider/ProxyPool.git
README.md里要求执行这条git命令,但我执行了很多次都说连接不上git服务器,因此我是通过本地浏览器下载后通过FTP上传到服务器上的。
3.4.2 解压项目
(base) [root@VM-4-17-centos ~]# unzip ProxyPool-master.zip
(base) [root@VM-4-17-centos ~]# cd ProxyPool-master
3.4.3 运行项目
接下来的步骤就有点蠢了,原本项目文件里的requirements.txt文件
可以一步完成这些步骤,但我这里直接执行这条命令
(base) [root@VM-4-17-centos ProxyPool-master]# pip install -r requirements.txt
发现会报错,因此我是通过多次启动项目,根据报错信息来安装缺少的依赖包,所有缺少的依赖包安装好后,项目就可以运行了。其中有的报错信息提示的依赖包和要安装的包名略有不同,如:fake_headers,实际安装的应该是 fake-headers
# 启动项目
(base) [root@VM-4-17-centos ProxyPool-master]# python run.py
# 看报错信息,安装依赖包,比如:
(base) [root@VM-4-17-centos ProxyPool-master]# pip install redis
(base) [root@VM-4-17-centos ProxyPool-master]# pip install environs
(base) [root@VM-4-17-centos ProxyPool-master]# pip install loguru
(base) [root@VM-4-17-centos ProxyPool-master]# pip install retrying
(base) [root@VM-4-17-centos ProxyPool-master]# pip install fake-headers
(base) [root@VM-4-17-centos ProxyPool-master]# pip install pyquery
(base) [root@VM-4-17-centos ProxyPool-master]# pip install aiohttp
当运行后不因为依赖包报错,并且输出大量日志信息时,说明项目成功运行起来了。
3.4.4 开启服务器5555和6379端口的访问
5555端口是ProxyPool项目
的IP代理API接口,访问服务器公网IP的5555端口的/random
路径即可获取一个筛选出来的可用免费代理IP。
6379端口是Redis数据库
的默认端口,开启后可以通过RDM软件
远程连接Redis数据库,检查当前数据库内的免费代理IP。
不同云服务器的端口开启方式不同,阿里云是安全组
,腾讯云是防火墙
。
下面是腾讯云设置后的样子
3.4.5 访问 /random 接口
看到这个画面就证明免费代理IP池已经运行起来了。
3.5 收尾工作
此时我重启了下服务器,并运行项目代码,但是发现Redis服务没有开启,奇怪,前面明明已经设置了开机自启Redis了,为什么现在又不行了呢?我检查了 /etc/rc.d/rc.local 文件,发现该文件被清空了,天啦噜,为了解决该问题,我又重新走了一遍上面的所有过程,并在3.3.3节的最后加入了备份该文件的操作,当再次发现该文件内容被清空时,只需要将之前备份的文件还原就可以了。
另外的python run.py命令会在SSH远程连接中断后,程序会停止运行
因此在项目目录下新建一个脚本文件 start.sh
(base) [root@VM-4-17-centos ProxyPool-master]# vi start.sh
nohup python run.py > log.txt 2>&1 &
保存后并执行
(base) [root@VM-4-17-centos ProxyPool-master]# chmod u+x start.sh
之后运行项目只需要执行命令,就可以了,并且所有的日志都会输出到 log.txt 文件中
# 启动程序
(base) [root@VM-4-17-centos ProxyPool-master]# ./start.sh
# 查看日志
(base) [root@VM-4-17-centos ProxyPool-master]# tail -f log.txt
保存后并执行
(base) [root@VM-4-17-centos ProxyPool-master]# chmod u+x start.sh
之后运行项目只需要执行命令,就可以了,并且所有的日志都会输出到 log.txt 文件中
# 启动程序
(base) [root@VM-4-17-centos ProxyPool-master]# ./start.sh
# 查看日志
(base) [root@VM-4-17-centos ProxyPool-master]# tail -f log.txt