Git Product home page Git Product logo

haipproxy's Introduction

HAipproxy

中文文档 | README

本项目所采集的IP资源都来自互联网,愿景是为大型爬虫项目提供一个高可用低延迟的高匿IP代理池

Features

  • 快速抓取代理IP
  • IP抓取和提取精准
  • IP来源丰富
  • 优良的IP校验器,并且容易根据自身需要扩展
  • 支持分布式部署
  • 架构设计灵活
  • MIT授权协议

Quick start

单机部署

服务端

  • 安装Python3和Redis。有问题可以阅读这篇文章的相关部分。

  • 根据Redis的实际配置修改项目配置文件config/settings.py中的REDIS_HOSTREDIS_PASSWORD等参数。

  • 安装scrapy-splash,并修改配置文件config/settings.py中的SPLASH_URL

  • 安装项目相关依赖

    pip install -r requirements.txt

  • 启动scrapy worker,包括代理IP采集器和校验器

    python crawler_booter.py --usage crawler

    python crawler_booter.py --usage validator

  • 启动调度器,包括代理IP定时调度和校验

    python scheduler_booter.py --usage crawler

    python scheduler_booter.py --usage validator

客户端(以Squid为例)

  • 安装squid,备份squid的配置文件并且启动squid,以ubuntu为例

    sudo apt-get install squid

    sudo sed -i 's/http_access deny all/http_access allow all/g'

    sudo cp /etc/squid/squid.conf /etc/squid/squid.conf.backup

    sudo service squid start

  • 根据操作系统修改项目配置文件config/settings.py中的SQUID_BIN_PATHSQUID_CONF_PATHSQUID_TEMPLATE_PATH等参数

  • 启动squid conf的定时更新程序

    sudo python squid_update.py

  • 使用squid作为代理中间层请求目标网站,默认代理URL为'http://squid_host:3128',用Python请求示例如下

    import requests
    proxies = {'https': 'http://127.0.0.1:3128'}
    resp = requests.get('https://httpbin.org/ip', proxies=proxies)
    print(resp.text)

Docker部署

  • 安装Docker

  • 安装docker-compose

    pip install -U docker-compose

  • 修改settings.py中的SPLASH_URLREDIS_HOST参数

  • 使用docker-compose启动各个应用组件

    docker-compose up

  • 使用squid作为代理中间层请求目标网站,默认代理URL为'http://squid_host:3128',用Python请求示例如下

    import requests
    proxies = {'https': 'http://127.0.0.1:3128'}
    resp = requests.get('https://httpbin.org/ip', proxies=proxies)
    print(resp.text)

注意事项

  • 本项目高度依赖Redis,除了消息通信和数据存储之外,IP校验使用了Redis中的多种数据结构。如果需要替换Redis,请自己 进行度量
  • 由于GFW的原因,某些网站需要通过科学上网才能进行访问和采集,如果用户无法访问墙外的网站,请将rules.py task_queue SPIDER_GFW_TASKSPIDER_AJAX_GFW_TASK的任务enable属性设置为0或者启动爬虫的时候指定爬虫类型为commonajax

    python crawler_booter.py --usage crawler common ajax

  • 相同代理IP,对于不同网站的代理效果可能大不相同。如果通用代理无法满足您的需求,您可以为特定网站编写代理IP校验器

如何贡献

  • 欢迎给项目提新feature
  • 如果发现项目某些环节有问题,欢迎提issue或者PR
  • 代理IP校验和筛选的策略仍有优化的空间,欢迎大家以issue或者PR交流探讨
  • 如果你发现了比较好的代理网站,欢迎分享或者直接以PR的方式分享

工作流程

开发者文档

为了方便用户针对自身需求进行定制化,haipproxy提供了丰富的文档支持。所有文档详见项目wiki

效果测试

以单机模式部署haipproxy测试代码,以知乎为目标请求站点, 每一万条成功请求为统计结果,实测抓取效果如下

请求量 时间 耗时 IP负载策略 客户端
0 2018/03/03 22:03 0 greedy py_cli
10000 2018/03/03 11:03 1 hour greedy py_cli
20000 2018/03/04 00:08 2 hours greedy py_cli
30000 2018/03/04 01:02 3 hours greedy py_cli
40000 2018/03/04 02:15 4 hours greedy py_cli
50000 2018/03/04 03:03 5 hours greedy py_cli
60000 2018/03/04 05:18 7 hours greedy py_cli
70000 2018/03/04 07:11 9 hours greedy py_cli
80000 2018/03/04 08:43 11 hours greedy py_cli

可见haipporxy的代理效果还算不错,在开始的时候可以达到1w/hour的请求量,几个小时候请求量请求量 降为了5k/hour。降低的结果可能有两个: (1)知乎校验器在把Init Queue中的代理消费完之后,由于是定 时任务,所以导致某段时间内新鲜的IP空缺。而免费IP大多数都是短效的,所以这段时间出现了IP的空缺;(2)由于 我们采用的是greedy模式调用IP,它的调用策略是: 高质量代理IP会一直被调用直至该代理IP不能用或者被封, 而低应速度IP会轮询调用。这也可能导致高质量IP的空缺。可见IP校验和调用策略还有很大的优化空间。希望志同 道合的朋友加入进来一起优化,这也挺有意思的。

测试代码见examples/zhihu

同类项目参考

本项目参考了Github上开源的各个爬虫代理的实现,感谢他们的付出,下面是笔者参考的所有项目,排名不分先后。

dungproxy

proxyspider

ProxyPool

proxy_pool

ProxyPool

IPProxyTool

IPProxyPool

proxy_list

proxy_pool

haipproxy's People

Contributors

resolvewang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.