Bez popisu

jhao 791f4bd527 [fix] :heavy_check_mark: fix python2 & python3 metaclass před 6 roky
api 429951060d [update] proxy check :camera: před 6 roky
db 421fd992f1 [fix] :heavy_check_mark: fix python2 & python3 metaclass před 6 roky
doc 7814f24150 [update] 为验证的代理不再入库,只储存useful :electric_plug: před 6 roky
docs 08467e769f [update] :heavy_check_mark: 2.1.0 před 6 roky
fetcher 04612be873 [update] :heavy_check_mark: freeProxy05 před 6 roky
handler 791f4bd527 [fix] :heavy_check_mark: fix python2 & python3 metaclass před 6 roky
helper 08467e769f [update] :heavy_check_mark: 2.1.0 před 6 roky
test 1a36662838 [update] :heavy_check_mark: test před 6 roky
util 421fd992f1 [fix] :heavy_check_mark: fix python2 & python3 metaclass před 6 roky
.gitignore dedf727671 [update] :heavy_check_mark: readthedocs před 6 roky
.travis.yml 446d252799 [update] travis-ci :boom: před 6 roky
Dockerfile b2abaf63ca [update] :heavy_check_mark: restruct před 6 roky
LICENSE 492b17df0f Create LICENSE před 9 roky
README.md 2b8c385e2b [docs] :heavy_check_mark: docs před 6 roky
_config.yml 3dfff5d7a1 Set theme jekyll-theme-time-machine před 9 roky
docker-compose.yml f417403e2e 修复docker-compose构建方式 před 6 roky
proxyPool.py c114560000 [update] :heavy_check_mark: print config před 6 roky
requirements.txt b2abaf63ca [update] :heavy_check_mark: restruct před 6 roky
setting.py c114560000 [update] :heavy_check_mark: print config před 6 roky
start.sh 2a190d494e [update] 重新整理项目结构 před 6 roky
test.py 1a36662838 [update] :heavy_check_mark: test před 6 roky

README.md

ProxyPool 爬虫代理IP池

Build Status Requirements Status Packagist GitHub contributors

______                        ______             _
| ___ \_                      | ___ \           | |
| |_/ / \__ __   __  _ __   _ | |_/ /___   ___  | |
|  __/|  _// _ \ \ \/ /| | | ||  __// _ \ / _ \ | |
| |   | | | (_) | >  < \ |_| || |  | (_) | (_) || |___
\_|   |_|  \___/ /_/\_\ \__  |\_|   \___/ \___/ \_____\
                       __ / /
                      /___ /

ProxyPool

运行项目

下载代码:
  • git clone

    git clone git@github.com:jhao104/proxy_pool.git
    
  • releases

    https://github.com/jhao104/proxy_pool/releases 下载对应zip文件
    
安装依赖:
pip install -r requirements.txt
更新配置:
# setting.py 为项目配置文件

# 配置API服务

HOST = "0.0.0.0"               # IP
PORT = 5000                    # 监听端口


# 配置数据库

DB_CONN = 'redis://:pwd@127.0.0.1:8888/0'


# 配置 ProxyFetcher

PROXY_FETCHER = [
    "freeProxy01",      # 这里是启用的代理抓取方法名,所有fetch方法位于fetcher/proxyFetcher.py
    "freeProxy02",
    # ....
]

启动项目:

# 如果已经具备运行条件, 可用通过proxyPool.py启动。
# 程序分为: schedule 调度程序 和 server Api服务

# 启动调度程序
python proxyPool.py schedule

# 启动webApi服务
python proxyPool.py server

Docker运行

docker pull jhao104/proxy_pool

docker run --env DB_CONN=redis://:password@ip:port/db -p 5010:5010 jhao104/proxy_pool:latest

使用

  • Api

启动web服务后, 默认配置下会开启 http://127.0.0.1:5010 的api接口服务:

api method Description arg
/ GET api介绍 None
/get GET 随机获取一个代理 None
/get_all GET 获取所有代理 None
/get_status 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

扩展代理

  项目默认包含几个免费的代理获取源,但是免费的毕竟质量有限,所以如果直接运行可能拿到的代理质量不理想。所以,提供了代理获取的扩展方法。

  添加一个新的代理源方法如下:

  • 1、首先在ProxyFetcher类中添加自定义的获取代理的静态方法, 该方法需要以生成器(yield)形式返回host:ip格式的代理,例如:

    
    class ProxyFetcher(object):
    # ....
    
    # 自定义代理源获取方法
    @staticmethod
    def freeProxyCustom1():  # 命名不和已有重复即可
    
        # 通过某网站或者某接口或某数据库获取代理
        # 假设你已经拿到了一个代理列表
        proxies = ["x.x.x.x:3128", "x.x.x.x:80"]
        for proxy in proxies:
            yield proxy
        # 确保每个proxy都是 host:ip正确的格式返回
    
  • 2、添加好方法后,修改setting.py文件中的PROXY_FETCHER项:

  在PROXY_FETCHER下添加自定义方法的名字:

PROXY_FETCHER = [
    "freeProxy01",    
    "freeProxy02",
    # ....
    "freeProxyCustom1"  #  # 确保名字和你添加方法名字一致
]

  schedule 进程会每隔一段时间抓取一次代理,下次抓取时会自动识别调用你定义的方法。

免费代理源

目前实现的采集免费代理网站有(排名不分先后, 下面仅是对其发布的免费代理情况, 付费代理测评可以参考这里):

| 厂商名称 | 状态 | 更新速度 | 可用率 | 是否被墙 | 地址 | | ----- | ---- | -------- | ------ | --------- | ----- | | 无忧代理 | 可用 | 几分钟一次 | * | 否 | 地址 | | 66代理 | 可用 | 更新很慢 | * | 否 | 地址 | | 西刺代理 | 可用 | 几分钟一次 | * | 否 | 地址| | 全网代理 | 可用 | 几分钟一次 | * | 否 | 地址| | 训代理 | 已关闭免费代理 | * | * | 否 | 地址| | 快代理 | 可用 |几分钟一次| * | 否 | 地址| | 云代理 | 可用 |几分钟一次| * | 否 | 地址| | IP海 | 可用 |几小时一次| * | 否 | 地址| | 免费IP代理库 | 可用 |快| * | 否 | 地址| | 中国IP地址 | 可用 |几分钟一次| * | 是 | 地址| | Proxy List | 可用 |几分钟一次| * | 是 | 地址| | ProxyList+ | 可用 |几分钟一次| * | 是 | 地址|

如果还有其他好的免费代理网站, 可以在提交在issues, 下次更新时会考虑在项目中支持。

问题反馈

  任何问题欢迎在Issues 中反馈,同时也可以到我的博客中留言。

  你的反馈会让此项目变得更加完美。

贡献代码

  本项目仅作为基本的通用的代理池架构,不接收特有功能(当然,不限于特别好的idea)。

  本项目依然不够完善,如果发现bug或有新的功能添加,请在Issues中提交bug(或新功能)描述,我会尽力改进,使她更加完美。

  这里感谢以下contributor的无私奉献:

  @kangnwh| @bobobo80| @halleywj| @newlyedward| @wang-ye| @gladmo| @bernieyangmh| @PythonYXY| @zuijiawoniu| @netAir| @scil| @tangrela| @highroom| @luocaodan| @vc5| @1again| @obaiyan @zsbh

Release Notes

changelog

AD

  最后, 开源不易, 有时间的小伙伴可以点下推广广告。

  AD