2 Star 5 Fork 0

FongR / 爬虫IP代理池

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

爬虫IP代理池

Build Status Requirements Status Packagist GitHub contributors

______                        ______             _
| ___ \_                      | ___ \           | |
| |_/ / \__ __   __  _ __   _ | |_/ /___   ___  | |
|  __/|  _// _ \ \ \/ /| | | ||  __// _ \ / _ \ | |
| |   | | | (_) | >  < \ |_| || |  | (_) | (_) || |___
\_|   |_|  \___/ /_/\_\ \__  |\_|   \___/ \___/ \_____\
                       __ / /
                      /___ /
介绍文档

下载安装

  • 下载源码:
git clone git@github.com:jhao104/proxy_pool.git

或者直接到https://github.com/jhao104/proxy_pool 下载zip文件
  • 安装依赖:
pip install -r requirements.txt
  • 配置Config.ini:
# Config.ini 为项目配置文件
# 配置DB
type = SSDB       # 如果使用SSDB或redis数据库,均配置为SSDB
host = localhost  # db host
port = 8888       # db port
name = proxy      # 默认配置

# 配置 ProxyGetter
freeProxyFirst  = 1  # 这里是启动的抓取函数,可在ProxyGetter/getFreeProxy.py 扩展
freeProxySecond = 1
....

# 配置 HOST (api服务)
ip = 127.0.0.1       # 监听ip,0.0.0.0开启外网访问
port = 5010          # 监听端口
# 上面配置启动后,代理api地址为 http://127.0.0.1:5010
  • 启动:
# 如果你的依赖已经安全完成并且具备运行条件,可以直接在Run下运行main.py
# 到Run目录下:
>>>python main.py

# 如果运行成功你应该看到有4个main.py进程

# 你也可以分别运行他们,
# 依次到Api下启动ProxyApi.py,Schedule下启动ProxyRefreshSchedule.py和ProxyValidSchedule.py即可.

使用

  启动过几分钟后就能看到抓取到的代理IP,你可以直接到数据库中查看,推荐一个SSDB可视化工具

  也可以通过api访问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/").content

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()
    while retry_count > 0:
        try:
            html = requests.get('https://www.example.com', proxies={"http": "http://{}".format(proxy)})
            # 使用代理访问
            return html
        except Exception:
            retry_count -= 1
    # 出错5次, 删除代理池中代理
    delete_proxy(proxy)
    return None

扩展代理

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

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

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

class GetFreeProxy(object):
    # ....

    # 你自己的方法
    @staticmethod
    def freeProxyCustom():  # 命名不和已有重复即可

        # 通过某网站或者某接口或某数据库获取代理 任意你喜欢的姿势都行
        # 假设你拿到了一个代理列表
        proxies = ["139.129.166.68:3128", "139.129.166.61:3128", ...]
        for proxy in proxies:
            yield proxy
        # 确保每个proxy都是 host:ip正确的格式就行
  • 2、添加好方法后,修改Config.ini文件中的[ProxyGetter]项:

  在Config.ini[ProxyGetter]下添加自定义的方法的名字:


[ProxyGetter]
;register the proxy getter function
freeProxyFirst  = 0  # 如果要取消某个方法,将其删除或赋为0即可
....
freeProxyCustom  = 1  # 确保名字和你添加方法名字一致

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

问题反馈

  任何问题欢迎在Issues 中反馈,如果没有账号可以去 我的博客中留言。

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

贡献代码

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

  本项目依然不够完善,如果发现bug或有新的功能添加,请在Issues中提交bug(或新功能)描述,在确认后提交你的代码。

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

  @kangnwh| @bobobo80| @halleywj| @newlyedward| @wang-ye| @gladmo| @bernieyangmh| @PythonYXY| @zuijiawoniu

MIT License Copyright (c) 2017 J_hao104 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/511311/proxy_pool.git
git@gitee.com:511311/proxy_pool.git
511311
proxy_pool
爬虫IP代理池
master

搜索帮助