3

docker快速启动proxy_pool项目 - z2n3

 1 year ago
source link: https://www.cnblogs.com/z2n3/p/17429309.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

docker快速启动proxy_pool项目

项目地址:https://github.com/jhao104/proxy_pool

0x01 搭建redis

该项目是基于redis的,所以我们首先需要一个redis,这里我还是选择用docker快速启动一个

  • 端口映射出来,可用于公网
docker run -d --name redis -p 6380:6379 redis --requirepass yourpassword
  • 不端口映射,直接在vps上内部连接
docker run -d --name redis  redis --requirepass yourpassword

0x02 查看容器内ip

如果你第一步中选择了映射,呢么就可以直接跳到0x03 启动项目 中直接运行了,如果不是,那么你就需要知道启动的实例的内网ip,但是当我们docker exec 进去的时候会发现并没有相关的ip addr的环境,懒得安装了,直接在本机用如下命令

docker inspect redis

这里的redis是容器的name,在结果中找到如下所示即为容器内网ip(可与本机通信)

2729785-20230524192414527-1521610507.png

本机可以ping通

2729785-20230524192414185-1063560546.png

0x03 启动项目

直接替换密码,ip以及响应的端口即可,选择映射出来的话再开放防火墙策略就可以通过公网ip访问了

docker run -d --env DB_CONN=redis://:[email protected]:6379/0 -p 5010:5010 jhao104/proxy_pool:latest

2729785-20230524192413656-1880310246.png

0x04 简单使用

启动web服务后, 默认配置下会开启 http://127.0.0.1:5010 的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

  如果要在爬虫代码中使用的话, 可以将此api封装成函数直接使用,例如:

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

剩下具体的可以去github看文档

0x05 后续

后续的话可以使用docker-compose,然后编写yml文件等等操作来实现更“自动化”

__EOF__

本文作者: zinc 本文链接: https://www.cnblogs.com/z2n3/p/17429309.html 关于博主: 评论和私信会在第一时间回复。或者直接私信我。 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处! 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK