Giter Site home page Giter Site logo

singularityben / boris-spider Goto Github PK

View Code? Open in Web Editor NEW

This project forked from boris-code/boris-spider

0.0 1.0 0.0 183 KB

boris-spider是一款使用Python语言编写的爬虫框架,于多年的爬虫业务中不断磨合而诞生,相比于scrapy,该框架更易上手,且又满足复杂的需求,支持分布式及批次采集。

Home Page: https://spider-doc.readthedocs.io

License: Other

Python 100.00%

boris-spider's Introduction

boris-spider

boris-spider是一款使用Python语言编写的爬虫框架,于多年的爬虫业务中不断磨合而诞生,相比于scrapy,该框架更易上手,且又满足复杂的需求,支持分布式及批次采集。

官方文档:https://spider-doc.readthedocs.io

特性

  1. 自动下载与重试异常请求,返回的response支持xpathcssre等解析方式。自动处理中文乱码;
  2. 支持 单进程爬虫分布式爬虫批次爬虫。批次爬虫封装了周期性采集数据的逻辑,批次开始时自动下发任务,抓取数据,统计任务处理速度,预估批次是否会超时,超时报警等;
  3. 随时终止、启动爬虫,任务不丢失不漏采
  4. 支持注册多模板,即可将多个网站的解析模板注册到同一个爬虫内,由该爬虫统一管理;
  5. 内置DebugSpiderDebugBatchSpider调试爬虫,调试代码更方便,且不会将调试过程中的数据入库,造成数据污染;
  6. 支持以模板的方式创建爬虫项目、解析模板、数据item;
  7. 数据自动入库,不需要编写Pipeline;

框架流程图

boris-spider -1-

模块说明:

  • spider 框架调度核心
  • parser_control 模版控制器,负责调度parser
  • collector 任务收集器,负责从任务队里中批量取任务到内存,以缓冲对任务队列数据库的访问频率及并发量
  • parser 数据解析器
  • start_request 初始任务下发函数
  • item_buffer 数据缓冲队列,批量将数据存储到数据库中
  • request_buffer 请求任务缓冲队列,批量将请求任务存储到任务队列中
  • request 数据下载器,封装了requests,用于从互联网上下载数据
  • response 数据返回体,封装了response, 支持xpath、css、re等解析方式。自动处理中文乱码

流程说明

  1. spider调度start_request生产任务
  2. start_request下发任务到request_buffer中
  3. spider调度request_buffer批量将任务存储到任务队列数据库中
  4. spider调度collector从任务队列中批量获取任务到内存队列
  5. spider调度parser_control从collector的内存队列中获取任务
  6. parser_control调度request请求数据
  7. request请求与下载数据
  8. request将下载后的数据给response,进一步封装
  9. 将封装好的response返回给parser_control(图示为多个parser_control,表示多线程)
  10. parser_control调度对应的parser,解析返回的response(图示多组parser表示不同的网站解析器)
  11. parser_control将parser解析到的数据item及新产生的request分发到item_bufferrequest_buffer
  12. spider调度item_bufferrequest_buffer将数据批量入库

环境要求:

  • Python 3.6.0+
  • Works on Linux, Windows, macOS

安装

From PyPi:

pip3 install boris-spider

From Git:

pip3 install git+https://github.com/Boris-code/boris-spider.git

快速上手

创建爬虫

spider create -p first_spider    

创建后的爬虫代码如下:

import spider


class FirstSpider(spider.SingleSpider):
    def start_requests(self, *args, **kws):
        yield spider.Request("https://www.baidu.com")

    def parser(self, request, response):
        # print(response.text)
        print(response.xpath('//input[@type="submit"]/@value').extract_first())


if __name__ == "__main__":
    FirstSpider().start()

直接运行,打印如下:

Thread-2|2020-05-19 18:23:41,128|request.py|get_response|line:283|DEBUG| 
                -------------- FirstSpider.parser request for ----------------
                url  = https://www.baidu.com
                method = GET
                body = {'timeout': 22, 'stream': True, 'verify': False, 'headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'}}
                
百度一下
Thread-2|2020-05-19 18:23:41,727|parser_control.py|run|line:415|INFO| parser 等待任务 ...
FirstSpider|2020-05-19 18:23:44,735|single_spider.py|run|line:83|DEBUG| 无任务,爬虫结束

福利

框架内的utils/tools.py模块下积累了作者多年的工具类函数,种类达到100+,且之后还会不定期更新,具有搬砖价值!

学习交流

想了解更多框架使用详情,可访问官方文档:https://spider-doc.readthedocs.io

如学习中遇到问题,可加下面的QQ群

群号:750614606

WechatIMG188

知识星球:

知识星球

星球会不定时分享爬虫技术干货,涉及的领域包括但不限于js逆向技巧、爬虫框架刨析、爬虫技术分享等

boris-spider's People

Contributors

boris-code avatar

Watchers

 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.