ProxyPool 爬虫代理IP池项目启动流程
一、准备工作
1、下载代码
githu地址:https://github.com/jhao104/proxy_pool.git
gitee地址:https://gitee.com/Colo330/proxy_pool.git
个人蓝奏云:https://wwgs.lanzoub.com/ilzLo1bgld4h
2、下载Redis
个人蓝奏云(win64):https://wwgs.lanzoub.com/injwY1bgldad
二、环境配置
1、分别解压上面两个压缩包
2、cd到代码根目录下,安装依赖
pip install -r requirements.txt
3、cd到Redis根目录下
4、修改Redis配置文件(redis.windows.conf文件内)
# requirepass foobared
requirepass 123456 //123456是设置的密码
5、Redis启动时指定配置文件
redis-server.exe redis.windows.conf
6、回到源代码路径下,修改代码的配置文件
# 配置数据库
DB_CONN = 'redis://:pwd@127.0.0.1:8888/0'
#这里只需要修改pwd为你的redis密码,例如:123456
#例如:DB_CONN = 'redis://:123456@127.0.0.1:8888/0'
三、项目启动
前提:确保Redis已经启动、
1、启动调度程序
python proxyPool.py schedule
2、启动webApi服务
python proxyPool.py server
四、Api使用
启动web服务后, 默认配置下会开启 http://127.0.0.1:5010 的api接口服务:
1、Api
api | method | Description | params |
---|---|---|---|
/ | GET | api介绍 | None |
/get | GET | 随机获取一个代理 | 可选参数: ?type=https 过滤支持https的代理 |
/pop | GET | 获取并删除一个代理 | 可选参数: ?type=https 过滤支持https的代理 |
/all | GET | 获取所有代理 | 可选参数: ?type=https 过滤支持https的代理 |
/count | GET | 查看代理数量 | None |
/delete | GET | 删除代理 | ?proxy=host:ip |
2、爬虫调用
import requests
def get_proxy():
return requests.get("http://127.0.0.1:5010/get/").json()
def delete_proxy(proxy):
requests.get("http://127.0.0.1:5010/delete/?proxy={}".format(proxy))
# your spider code
def getHtml():
# ....
retry_count = 5
proxy = get_proxy().get("proxy")
while retry_count > 0:
try:
html = requests.get('http://www.example.com', proxies={"http": "http://{}".format(proxy)})
# 使用代理访问
return html
except Exception:
retry_count -= 1
# 删除代理池中代理
delete_proxy(proxy)
return None