文章来源:https://github.com/rmax/scrapy-redis

Scrapy-Redis

Requirements

  • Python 2.7, 3.4 or 3.5
  • Redis >= 2.8
  • Scrapy >= 1.1
  • redis-py >= 2.10

Usage

Use the following settings in your project:

# Enables scheduling storing requests queue in redis.
SCHEDULER = "scrapy_redis.scheduler.Scheduler" # Ensure all spiders share same duplicates filter through redis.
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter" # Default requests serializer is pickle, but it can be changed to any module
# with loads and dumps functions. Note that pickle is not compatible between
# python versions.
# Caveat: In python .x, the serializer must return strings keys and support
# bytes as values. Because of this reason the json or msgpack module will not
# work by default. In python .x there is no such issue and you can use
# 'json' or 'msgpack' as serializers.
#SCHEDULER_SERIALIZER = "scrapy_redis.picklecompat" # Don't cleanup redis queues, allows to pause/resume crawls.
#SCHEDULER_PERSIST = True # Schedule requests using a priority queue. (default)
#SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.PriorityQueue' # Alternative queues.
#SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.FifoQueue'
#SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.LifoQueue' # Max idle time to prevent the spider from being closed when distributed crawling.
# This only works if queue class is SpiderQueue or SpiderStack,
# and may also block the same time when your spider start at the first time (because the queue is empty).
#SCHEDULER_IDLE_BEFORE_CLOSE = # Store scraped item in redis for post-processing.
ITEM_PIPELINES = {
'scrapy_redis.pipelines.RedisPipeline':
} # The item pipeline serializes and stores the items in this redis key.
#REDIS_ITEMS_KEY = '%(spider)s:items' # The items serializer is by default ScrapyJSONEncoder. You can use any
# importable path to a callable object.
#REDIS_ITEMS_SERIALIZER = 'json.dumps' # Specify the host and port to use when connecting to Redis (optional).
#REDIS_HOST = 'localhost'
#REDIS_PORT = # Specify the full Redis URL for connecting (optional).
# If set, this takes precedence over the REDIS_HOST and REDIS_PORT settings.
#REDIS_URL = 'redis://user:pass@hostname:9001' # Custom redis client parameters (i.e.: socket timeout, etc.)
#REDIS_PARAMS = {}
# Use custom redis client class.
#REDIS_PARAMS['redis_cls'] = 'myproject.RedisClient' # If True, it uses redis' ``SPOP`` operation. You have to use the ``SADD``
# command to add URLs to the redis queue. This could be useful if you
# want to avoid duplicates in your start urls list and the order of
# processing does not matter.
#REDIS_START_URLS_AS_SET = False # Default start urls key for RedisSpider and RedisCrawlSpider.
#REDIS_START_URLS_KEY = '%(name)s:start_urls' # Use other encoding than utf- for redis.
#REDIS_ENCODING = 'latin1'

Feeding a Spider from Redis

The class scrapy_redis.spiders.RedisSpider enables a spider to read the urls from redis. The urls in the redis queue will be processed one after another, if the first request yields more requests, the spider will process those requests before fetching another url from redis.

For example, create a file myspider.py with the code below:

from scrapy_redis.spiders import RedisSpider

class MySpider(RedisSpider):
name = 'myspider' def parse(self, response):
# do stuff
pass

Then:

  1. run the spider:

scrapy runspider myspider.py

2.push urls to redis:

redis-cli lpush myspider:start_urls http://google.com

Note

These spiders rely on the spider idle signal to fetch start urls, hence it may have a few seconds of delay between the time you push a new url and the spider starts crawling it.

分布式爬虫的原理就是共享调度器(爬取队列),利用scrapy_redis重写调度器

spider文件夹下爬虫文件中的name,为lpush到redis中的key值,如果redis不为空,爬虫文件就会从redis中根据key值获取到对应的url运行爬虫。

分布式爬虫的实现

1.redis 服务
2.确保scrapy-redis 环境已经安装

3.确定单机scrapy 能够正常运行

4.配置setting.py 文件

setting.py 配置代码如下

# url指纹过滤器
'DUPEFILTER_CLASS' : "scrapy_redis.dupefilter.RFPDupeFilter",
# 调度器
'SCHEDULER' : "scrapy_redis.scheduler.Scheduler",
# 设置爬虫是否可以中断
'SCHEDULER_PERSIST' : True,
'SCHEDULER_QUEUE_CLASS' : "scrapy_redis.queue.SpiderQueue", # 按照队列模式
# redis 连接配置
'REDIS_HOST' : '10.15.112.29',
'REDIS_PORT' :,
#密码和选择的库号
'REDID_PARAMS' = {
'password': '',
'db' : ,
}
## 配置redis管道文件,权重数字相对最大
'ITEM_PIPELINES' = {
'scrapy_redis.pipelines.RedisPipeline': , # redis管道文件,自动把数据加载到redis
}
SCHEDULER是任务分发与调度,把所有的爬虫开始的请求都放在redis里面,所有爬虫都去redis里面读取请求。 DUPEFILTER_CLASS是去重队列,负责所有请求的去重,REDIS_START_URLS_AS_SET指的是使用redis里面的set类型(简单完成去重),如果你没有设置,默认会选用list。

5 接下来就是设置你的爬虫文件了
在spider文件下找你写好的爬虫文件

然后导入

from scrapy_redis.spiders import RedisCrawlSpider

你的class类要继承RedisCrawlSpider

class BosszpSpider(RedisCrawlSpider):

然后设置属性
设置爬虫的rediskey(这个个自己取名,要和redis的键名一样)

redis_key = 'boss_url'

接下来连接redis

启动redis服务端的命令是redis-server ./redis.cfon

键名就是你爬虫属性里设置的名字

值就是你首次请求的url

配置完后就可以启动你的爬虫了

命令 scrapy runspider 爬虫文件名

scrapy_redis分布式爬虫的更多相关文章

  1. scrapy分布式爬虫scrapy_redis二篇

    =============================================================== Scrapy-Redis分布式爬虫框架 ================ ...

  2. scrapy分布式爬虫scrapy_redis一篇

    分布式爬虫原理 首先我们来看一下scrapy的单机架构:     可以看到,scrapy单机模式,通过一个scrapy引擎通过一个调度器,将Requests队列中的request请求发给下载器,进行页 ...

  3. python3下scrapy爬虫(第十三卷:scrapy+scrapy_redis+scrapyd打造分布式爬虫之配置)

    之前我们的爬虫都是单机爬取,也是单机维护REQUEST队列, 看一下单机的流程图: 一台主机控制一个队列,现在我要把它放在多机执行,会产生一个事情就是做重复的爬取,毫无意义,所以分布式爬虫的第一个难点 ...

  4. Centos7__Scrapy + Scrapy_redis 用Docker 实现分布式爬虫

    原理:其实就是用到redis的优点及特性,好处自己查--- 1,scrapy 分布式爬虫配置: settings.py BOT_NAME = 'first' SPIDER_MODULES = ['fi ...

  5. 利用scrapy_redis实现分布式爬虫

    介绍 Scrapy框架不支持分布式,所以需要将一些关键代码进行修改使之支持分布式.scrapy-redis相当于一个插件,用来替换scrapy中的一些模块,使得scrapy支持分布式.github地址 ...

  6. scrapy-redis 分布式爬虫

    为什么要学? Scrapy_redis在scrapy的基础上实现了更多,更强大的功能. 有哪些功能体现? request去重.爬虫持久化.实现分布式爬虫.断点续爬(带爬取的request存在redis ...

  7. Scrapy+Scrapy-redis+Scrapyd+Gerapy 分布式爬虫框架整合

    简介:给正在学习的小伙伴们分享一下自己的感悟,如有理解不正确的地方,望指出,感谢~ 首先介绍一下这个标题吧~ 1. Scrapy:是一个基于Twisted的异步IO框架,有了这个框架,我们就不需要等待 ...

  8. 基于scrapy-redis的分布式爬虫

    一.介绍 1.原生的scrapy框架 原生的scrapy框架是实现不了分布式的,其原因有: 1. 因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls ...

  9. 基于scrapy-redis分布式爬虫的部署

    redis分布式部署 1.scrapy框架是否可以自己实现分布式? - 不可以.原因有二. 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls ...

随机推荐

  1. Halcon C# 联合编程问题(三)

    因为之前遇到的那个halcon处理的图片要转换成ImageSource的问题,迟迟没有找到好的解决方案, 于是决定直接在wpf中使用halcon提供的HWindowControlWPF,用于显示图片. ...

  2. 子字符串查找之————关于KMP算法你不知道的事

    写在前面: (阅读本文前需要了解KMP算法的基本思路.另外,本着大道至简的思想,本文的所有例子都会做从头到尾的讲解) 作者翻阅了大量网上现有的KMP算法博客,发现广为流传的竟然是一种不完整的KMP算法 ...

  3. 数据结构(十六)模式匹配算法--Brute Force算法和KMP算法

    一.模式匹配 串的查找定位操作(也称为串的模式匹配操作)指的是在当前串(主串)中寻找子串(模式串)的过程.若在主串中找到了一个和模式串相同的子串,则查找成功:若在主串中找不到与模式串相同的子串,则查找 ...

  4. js奥义:原型与原型链(2)

    回顾:上一篇讲了原型对象与prototype和__proto__(传送门 )三者之间的关系 三:constructor constructor [kənˈstrʌktə(r)] :构造器,  这是子类 ...

  5. 元素“context:component-scan”的前缀“context”未绑定

    首先报这个错误,你得明白,是什么原因导致的? 答:未引入命名空间,和约束文件 解决方法(加上标红色标记): <?xml version="1.0" encoding=&quo ...

  6. linux C进程常用操作

    不登高山,不知天之高也: 不临深溪,不知地之厚也. 荀子<劝学> linux应用层主要是一个个独立任务的进程在运行,但是很多时候,在工作中我们可能很少去重新写一个进程, 大部分的工作都是分 ...

  7. 【IntelliJ IDEA】 常用快捷键列表

    1.常用Shortcut F2 或Shift+F2 高亮错误或警告快速定位 Ctrl+Up/Down 光标跳转到第一行或最后一行下 Ctrl+B 快速打开光标处的类或方法  CTRL+ALT+B  找 ...

  8. CSS汇总之CSS选择器

    要使用css对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器. 一.通配符选择器 语法:*{ } 说明:通配符选择器可以选择页面上所有的html标签(包括body,h ...

  9. 最新JetBrains PyCharm 使用教程--下载安装Python库(五)

    最新JetBrains PyCharm 下载安装Python库 ​

  10. Java描述设计模式(19):模板方法模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 通常一款互联网应用的开发流程如下:业务需求,规划产品,程序开发,测试交付.现在基于模板方法模式进行该过程描述. public c ...