scrapy-redis基础和介绍
一、scrapy-redis(0.6)依赖的环境
Scrapy >= 1.0.0 #终于过了1版本,这个太重要了,总算坑小了点,感谢那些为了解决各种scrapy与scrapy-redis不兼容做出了贡献的开发者和博主。
redis-py >= 2.10.0
redis server >= 2.8.0
0.6版本的主要更新内容是更新代码以支持Scrapy 1.0; 增加了-a domain=... option for example spiders.
二、scrapy-redis的作用和特点
作用:scrapy-redis为Scrapy提供Redis-backed组件
特点: 可以启动多个爬虫实例共享一个单一的 redis队列。是最适合广泛的多域爬虫。
分布式的post处理。scrapy到的items放入一个redis队列意味着可以分享这个items队列,并在其中启用足够多的post处理进程。
三、scrapy和scrapy-redis的区别与组件的意义
Scheduler Duplication Filter Item PipelineBase Spider
自己取得名字) 传送门:queuelib/queuelib/queue.py源码{
priority0:队列0priority1:队列2priority2:队列2
}
- </pre></div></blockquote><pre name="code" class="plain">def request_seen(self, request):
- #self.figerprints就是一个指纹集合
- fp = self.request_fingerprint(request)
- if fp in self.fingerprints:#这就是判重的核心操作。
- return True
- self.fingerprints.add(fp)
- ......
在scrapy-redis中去重是由Duplication Filter组件来实现的。
四、最快的安装和启用
安装:
- $ pip install scrapy-redis
- 或者
- $ git clone https://github.com/darkrho/scrapy-redis.git
- $ cd scrapy-redis
- $ python setup.py install
在 settings.py 中启用组件们:
- </pre></blockquote><pre name="code" class="plain"># Enables scheduling storing requests queue in redis.
- SCHEDULER = "scrapy_redis.scheduler.Scheduler"
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_16_2220405" name="code" class="python"># Don't cleanup redis queues, allows to pause/resume crawls.
- SCHEDULER_PERSIST = True
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_19_5349541" name="code" class="python"># Schedule requests using a priority queue. (default)
- SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderPriorityQueue'
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_22_4193298" name="code" class="python"># Schedule requests using a queue (FIFO).
- SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderQueue'
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_25_5337568" name="code" class="python"># Schedule requests using a stack (LIFO).
- SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.SpiderStack'
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_28_6547876" name="code" class="python"># 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 = 10
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_33_5980278" name="code" class="python"># Store scraped item in redis for post-processing.
- ITEM_PIPELINES = {
- 'scrapy_redis.pipelines.RedisPipeline': 300
- }
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_38_8651283" name="code" class="python"># Specify the host and port to use when connecting to Redis (optional).
- REDIS_HOST = 'localhost'
- REDIS_PORT = 6379
- </pre></div></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><div><span style="white-space:pre"></span><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_42_1638716" name="code" class="python"># 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'
五、通过redis来喂饱爬虫们~
scrapy_redis.spiders.RedisSpider类启用了爬虫通过redis得到urls,这些在redis队列中的爬虫将会一个接一个的被处理,!!如果第一个request产生了更多的request,爬虫会先处理这些请求,再从redis队列中抓取其他url。
上面偷懒,这里举个栗子:创建 couxiaoxiao.py
- from scrapy_redis.spiders import RedisSpider
- </pre></div><div><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_47_2390110" name="code" class="python">class MySpider(RedisSpider):
- name = 'myspider'
- </pre></div><div><pre code_snippet_id="1682225" snippet_file_name="blog_20160513_50_2662327" name="code" class="python"> def parse(self, response):
- # do stuff
- pass
运行爬虫
- scrapy runspider myspider.py
向redis中装入url们
- redis-cli lpush myspider:start_urls http://xiaowangzhi.com
scrapy-redis基础和介绍的更多相关文章
- Redis项目实战---应用及理论(上)---redis基础知识介绍
redis(Remote Dictionary Server) 一.原理及特性层面: 1.优势: 1)数据加载在内存中,执行速度快, 数据结构类似于HashMap,HashM ...
- Redis基础知识补充及持久化、备份介绍(二)--技术流ken
Redis知识补充 在上一篇博客<Redis基础认识及常用命令使用(一)--技术流ken>中已经介绍了redis的一些基础知识,以及常用命令的使用,本篇博客将补充一些基础知识以及redis ...
- Redis基础知识补充及持久化、备份介绍
Redis知识补充 在上一篇博客<Redis基础认识及常用命令使用(一)–技术流ken>中已经介绍了redis的一些基础知识,以及常用命令的使用,本篇博客将补充一些基础知识以及redis持 ...
- [.net 面向对象程序设计深入](14)Redis——基础
[.net 面向对象程序设计深入](14)Redis——基础 很长一段时间没更新博客了,坚持做一件事,真不是件容易的事,后面我会继续尽可能的花时间更新完这个系列文章. 因这个系列的文章涉及的范围太大了 ...
- [.net 面向对象程序设计深入](36)Redis——基础
[.net 面向对象程序设计深入](36)Redis——基础 很长一段时间没更新博客了,坚持做一件事,真不是件容易的事,后面我会继续尽可能的花时间更新完这个系列文章. 因这个系列的文章涉及的范围太大了 ...
- Redis基础用法、高级特性与性能调优以及缓存穿透等分析
一.Redis介绍 Redis是一个开源的,基于内存的结构化数据存储媒介,可以作为数据库.缓存服务或消息服务使用.Redis支持多种数据结构,包括字符串.哈希表.链表.集合.有序集合.位图.Hype ...
- Redis高级特性介绍及实例分析
转自:http://www.jianshu.com/p/af7043e6c8f9 Redis基础类型回顾 String Redis中最基本,也是最简单的数据类型.注意,VALUE既可以是简单的St ...
- redis基础之python连接redis(五)
前言 前面介绍了在数据库命令行直接操作redis,现在学习使用python的redis包来操作redis,本人安装的是redis==2.10.6: 系列文章 redis安装和配置 redis命令行操作 ...
- REDIS基础笔记
Redis基础笔记 资源链接 简介 简介 安装 五种数据类型及相应命令 1. 字符串类型 2. 散列类型 3. 列表类型 4. 集合类型 5. 有序集合 其他 事务 SORT 生存时间 任务队列 发布 ...
- 消息队列介绍、RabbitMQ&Redis的重点介绍与简单应用
消息队列介绍.RabbitMQ&Redis的重点介绍与简单应用 消息队列介绍.RabbitMQ.Redis 一.什么是消息队列 这个概念我们百度Google能查到一大堆文章,所以我就通俗的讲下 ...
随机推荐
- 第一个html文件
1.新建记事本文件,后缀改为.html 2.添加: <html> <head> <title>jude`s first web</title> & ...
- HttpClient 的使用
HttpClient使用: maven: <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient ...
- 【第二十七章】 springboot + zipkin(brave-okhttp实现)
本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...
- Unity3D学习笔记(二十三):事件接口、虚拟摇杆、层级管理和背包系统
事件接口 IDragHandler(常用):鼠标按下拖动时执行(只要鼠标在拖动就一直执行) IDropHandler:对象拖动结束时,如果鼠标在物体的范围内,执行一次(依赖于IDragHandler存 ...
- C++小结:迟到的小结和重新起航的故事
迟到的小结和重新起航的故事 有关这个学期的故事,随着这个学期的结束也划上了一个句号. 正如之前博客里面(还是空间里面)提到的,在这个过程中的收获比最后考试的结果更重要. 就像这次的计算器,也许会对最后 ...
- 初探 Yii2 的测试模式 index-test.php
有没有发现高级版每个应用的 web 目录下有两个入口文件,一个index.php 一个 index-test.php通过init.bat可以切换到调试模式和产品模式,这两个模式相信同学们都很熟悉了,那 ...
- go 通道
1. package main import "fmt" func sum(s []int, c chan int) { sum := for _, v := range s { ...
- ubuntu16.04, git 的配置
1.下载git:sudo apt-get install git 2.生成公钥:ssh-keygen -t rsa -C '你的邮箱' ,不停回车即可 3. mkdir test cd te ...
- MongoDB(课时5 数据查询)
3.4.2 数据查询 对于数据的查询操作核心语法: db.集合名称.find({查询条件}, {设置显示的字段}) 范例:没查询条件 db.info.find() 范例:有查询条件,查询出url为&q ...
- ScriptableObjec 的简单使用
1.ScriptableObject的创建(一): using System.Collections; using System.Collections.Generic; using UnityEng ...