Scrapy_redis(三十二)

1、Scrapy_redis(先进行Scrapy_redis,在把项目部署到Scrapyd中)

1、1 特点和架构
  • scrapy_redis是一个基于Redis的Scrapy组件,用于scrapy项目的分布式部署和开发。
  • 特点:
    (1)分布式爬取(使各个主机能够进行同一件事情,scrapy本身不支持分布式爬取)
    你可以启动多个spider对象,互相之间共享有一个redis的request队列。最适合多个域名的广泛内容的爬取。
    (2)分布式数据处理
    爬取到的item数据被推送到redis中,这意味着你可以启动尽可能多的item处理程序。
    (3)scrapy即插即用(Scrapy_redis是scrapy的一个插件,依托于scrapy)
    scrapy调度程序+过滤器,项目管道,base spidre,使用简单。


注意:在scrapy的基本流程中,调度器是把请求放到了内存中;而这里是把请求放到了Redis数据库中,以便存在Redis数据中的请求队列被多个机器共享

1、2 安装与使用
  • 一般通过pip安装Scrapy-redis:
    pip install scrapy-redis

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

  • scrapy-redis的使用非常简单,几乎可以并不改变原本scrapy项目的代码,只用做少量设置。

1、3 常用设置
  • 启用调度将请求存储进redis,是在settings中做以下配置(scrapy_redis配置,配置调度器)---必须配置项。
    SCHEDULER = "scrapy_redis.scheduler.Scheduler"

  • 确保所有spider通过redis共享相同的重复过滤。(配置重复过滤器)--必须配置项
    DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

  • 指定连接到Redis时要使用的主机和端口。(对redis数据库进行配置)--必须配置项
    REDIS_HOST = 'localhost'
    REDIS_PORT = 6379

  • 不清理redis队列,允许暂停/恢复抓取。----可选配置项
    SCHEDULER_PERSIST = True

官方文档:https://scrapy-redis.readthedocs.io/en/stable/

1、4 redis中存储的数据
  • spidername:items
    list类型,保存爬虫获取到的数据item内容是json字符串。

  • spidername:dupefilter
    set类型,用于爬虫访问的URL去重内容是40个字符的url的hash字符串

  • spidername:start_urls
    list类型,用于接收redisspider启动时的第一个url

  • spidername:requests
    zset类型,用于存放requests等待调度。内容是requests对象的序列化字符串。

2、项目案例(爬取中国图书网中的部分书籍信息)

2、1 booksys.py
# -*- coding: utf-8 -*-
import scrapy
from lxml import etree
from ..items import LibrarysystemItem


class BooksysSpider(scrapy.Spider):
    name = 'booksys'
    # allowed_domains = ['http://www.bookschina.com/']
    # start_urls = ['http://www.bookschina.com/kinder/63000000/','http://www.bookschina.com/kinder/51000000/','http://www.bookschina.com/kinder/27000000/','http://www.bookschina.com/kinder/31000000/','http://www.bookschina.com/kinder/35000000/','http://www.bookschina.com/kinder/64000000/']
    start_urls = ['http://www.bookschina.com/kinder/31000000/']

    def parse(self, response):
        sa111 = response.xpath('//div[@class="w1200"]/div[@id="container"]/div[@class="listMain clearfix"]/div[@class="listLeft"]/div[@class="bookList"]/ul/li')
        if sa111:
            for content in sa111:
                li = []
                content = content.extract()
                content = etree.HTML(content)
                item = LibrarysystemItem()
                item = dict(item)
                item['imagename'] = content.xpath('//div[@class="cover"]/a/img/@src')[0].strip()
                item['bookname'] = content.xpath('//div[@class="infor"]/h2/a/text()')[0].strip()
                item['author'] = content.xpath('//div[@class="otherInfor"]/a/text()')[0].strip()
                item['discount'] = content.xpath('//div[@class="priceWrap"]/span[@class="sellPrice"]/text()')[0].strip()
                item['price'] = content.xpath('//div[@class="priceWrap"]/del/text()')[0].strip()
                item['linkaddress'] = 'http://www.bookschina.com'+content.xpath('//div[@class="infor"]/h2/a/@href')[0].strip()
                item['introduction'] = content.xpath('//p[@class="recoLagu"]/text()')
                if item['introduction'] == li:
                    item['introduction'] = "此书无简介"
                else:
                    item['introduction'] = item['introduction'][0].strip().replace('\r', '')
                yield item
            next_url = response.xpath('//div[@class="pagination"]/div[@class="paging"]/ul/li[@class="next"]')
            next_url = next_url.xpath('//li[@class="next"]/a/@href').extract_first()
            yield scrapy.Request('http://www.bookschina.com/%s'% next_url)

2、2 items.py
# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class LibrarysystemItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    # image = scrapy.Field()
    # bookname1 = scrapy.Field()
    # author = scrapy.Field()
    # discountprice = scrapy.Field()
    # originalprice = scrapy.Field()
    # linkaddress = scrapy.Field()
    # introduce = scrapy.Field()
    imagename = scrapy.Field()
    bookname = scrapy.Field()
    author = scrapy.Field()
    discount = scrapy.Field()
    price = scrapy.Field()
    linkaddress = scrapy.Field()
    introduction = scrapy.Field()

2、3 middlewares.py
# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals
import random


class LibrarysystemSpiderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the spider middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_spider_input(self, response, spider):
        # Called for each response that goes through the spider
        # middleware and into the spider.

        # Should return None or raise an exception.
        return None

    def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, dict or Item objects.
        for i in result:
            yield i

    def process_spider_exception(self, response, exception, spider):
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Response, dict
        # or Item objects.
        pass

    def process_start_requests(self, start_requests, spider):
        # Called with the start requests of the spider, and works
        # similarly to the process_spider_output() method, except
        # that it doesn’t have a response associated.

        # Must return only requests (not items).
        for r in start_requests:
            yield r

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class LibrarysystemDownloaderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return None

    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class RandomUserAgentMiddleWare(object):
    def __init__(self, user_agents):
        self.user_agents = user_agents

    @classmethod
    def from_crawler(cls,crawler):
        s=cls(user_agents=crawler.settings.get('MY_USER_AGENT'))
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        agent=random.choice(self.user_agents)
        request.headers['user_agent']=agent
        return None

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)

2、4 pipelines.py
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
from .items import LibrarysystemItem
import pymysql
import logging

logger = logging.getLogger(__name__)


class LibrarysystemPipeline(object):
    #做数据持久化
    def process_item(self, item, spider):
        # self.f.write(json.dumps(item, ensure_ascii=False))
        # self.f.write('\n')
        # return item
        if item:
            sql = 'select ID from booktable WHERE bookname=%s AND  author=%s'
            self.cursor.execute(sql, (item['bookname'], item['author']))
            if self.cursor.fetchone():
                pass
            else:
                try:
                    print("正在爬取中"+item['linkaddress']+"网站的书籍信息")
                    sql='insert into booktable(imagename,bookname, author,discount,price,linkaddress,introduction) VALUES(%s,%s,%s,%s,%s,%s,%s)'
                    self.cursor.execute(sql, (
                        item['imagename'],
                        item['bookname'],
                        item['author'],
                        item['discount'],
                        item['price'],
                        item['linkaddress'],
                        item['introduction']
                    ))
                    self.conn.commit()
                except Exception as e:
                    self.conn.rollback()
                    print("出现一下错误:")
                    logger.warning('书籍信息写入错误 url =%s %s' % (item['linkaddress'], e))
        else:
            logger.info('不存在')

    def open_spider(self, spider):
        data_config = spider.settings['DATABASE_CONFIG']
        if data_config['type'] == 'mysql':
            self.conn = pymysql.connect(**data_config['config'])
            self.cursor = self.conn.cursor()
            spider.conn = self.conn
            spider.cursor = self.cursor

    def close_spider(self, spider):
        data_config = spider.settings['DATABASE_CONFIG']
        if data_config['type'] == 'mysql':
            self.cursor.close()
            self.conn.close()
2、5 scrapy.cfg

[settings]
default = librarysystem.settings

[deploy:librarysystem]
url = http://192.168.212.131:6800/
project = librarysystem

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容