1、如何将一个scrapy爬虫项目修改成为一个简单的分布式爬虫项目

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

只用修改scrapy项目的两个文件就可以了

一个是爬虫组件文件:

# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider # 自定义爬虫类的继承类不再是scrapy.spiders下面的爬虫类,
# 而是scrapy-redis.spiders下面的爬虫类
class DistributedSpiderSpider(RedisSpider):
name = 'distributed_spider'
allowed_domains = ['wh.lianjia.com']
# 将start_urls替换为redis_key
# start_urls = ['https://wh.lianjia.com/ershoufang/']
redis_key = "myspider:start_urls" def parse(self, response):
print(response.meta)
item = dict()
item["province"] = "湖北"
item["city"] = "武汉"
blocks = response.xpath("//div[@class='position']/dl[2]/dd/div[1]/div/a")
for block in blocks:
title = blocks.xpath("./@title").get()
url = "https://wh.lianjia.com" + block.xpath("./@href").get() print(title, url, "=====================================") yield scrapy.Request(url=url, callback=self.parse_block, meta={"item": item}) def parse_block(self, response):
print(response.meta)
url = response.url + "pg%dco32/"
index = 1
while index < 100:
new_url = url % index
print(new_url)
yield scrapy.Request(url=new_url, callback=self.parse_item, meta={"item": response.meta["item"]})
index += 1 def parse_item(self, response):
print(response.meta)
sellLinks = response.css("ul.sellListContent>li>a")
for link in sellLinks:
url = link.xpath("./@href").get()
print(url)
yield scrapy.Request(url=url, callback=self.parse_detail, meta={"item": response.meta["item"]}) def parse_detail(self, response):
item = response.meta["item"]
print(response.meta)
item["url"] = response.url
item["block"] = response.css("div.areaName>span.info").xpath("./a[1]/text()").get().strip()
item["smallBlock"] = response.css("div.areaName>span.info").xpath("./a[2]/text()").get().strip()
item["price"] = response.xpath("//span[@class='total']/text()").get().strip() + "万"
item["unitPrice"] = response.xpath("//span[@class='unitPriceValue']/text()").get().strip() + "元/平米"
print(item)

另一个是设置文件(settings.py):

在设置文件中添加几个设置项就可以了

SCHEDULER = "scrapy_redis.scheduler.Scheduler"

DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

ITEM_PIPELINES = {
'DistributedSpider.pipelines.DistributedspiderPipeline': 300,
'scrapy_redis.pipelines.RedisPipeline': 400
} REDIS_HOST = "localhost" # 要连接的redis数据库的地址 REDIS_PORT = "" # redis数据库启动时的默认端口

  

scrapy爬虫-scrapy-redis分布式的更多相关文章

  1. [scrapy-redis] 将scrapy爬虫改造成分布式爬虫 (2)

    1. 修改redis设置 redis默认处在protection mode, 修改/etc/redis.conf, protected-mode no, 或者给redis设置密码, 将bind 127 ...

  2. <scrapy爬虫>scrapy命令行操作

    1.mysql数据库 2.mongoDB数据库 3.redis数据库 1.创建项目 scrapy startproject myproject cd myproject 2.创建爬虫 scrapy g ...

  3. python - scrapy 爬虫框架 ( redis去重 )

    1.  使用内置,并加以修改 ( 自定义 redis 存储的 keys ) settings 配置 # ############### scrapy redis连接 ################# ...

  4. 自己动手实现爬虫scrapy框架思路汇总

    这里先简要温习下爬虫实际操作: cd ~/Desktop/spider scrapy startproject lastspider # 创建爬虫工程 cd lastspider/ # 进入工程 sc ...

  5. (5)分布式下的爬虫Scrapy应该如何做-windows下的redis的安装与配置

    软件版本: redis-2.4.6-setup-64-bit.exe — Redis 2.4.6 Windows Setup (64-bit) 系统: win7 64bit 本篇的内容是为了给分布式下 ...

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

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

  7. 爬虫---scrapy分布式和增量式

    分布式 概念: 需要搭建一个分布式的机群, 然后在每一台电脑中执行同一组程序, 让其对某一网站的数据进行联合分布爬取. 原生的scrapy框架不能实现分布式的原因 调度器不能被共享, 管道也不能被共享 ...

  8. (8)分布式下的爬虫Scrapy应该如何做-图片下载(源码放送)

      转载主注明出处:http://www.cnblogs.com/codefish/p/4968260.html 在爬虫中,我们遇到比较多需求就是文件下载以及图片下载,在其它的语言或者框架中,我们可能 ...

  9. (2)分布式下的爬虫Scrapy应该如何做-关于对Scrapy的反思和核心对象的介绍

    本篇主要介绍对于一个爬虫框架的思考和,核心部件的介绍,以及常规的思考方法: 一,猜想 我们说的爬虫,一般至少要包含几个基本要素: 1.请求发送对象(sender,对于request的封装,防止被封) ...

随机推荐

  1. Different Gene Frequencies in the Two Sexes

    I.7 Different Gene Frequencies in the Two Sexes 假设存在一种基因仅在第一代亲代的不同性别之间的概率有区别,比如,A 在male中频率是Pm,a是(1-P ...

  2. windowsserver的应用升级部署坑

    jar文件的后缀名要打开显示,否者备份的‘.bak20190820’可能后面还是jar可执行文件,以至于mybatis这类xml映射器的namespace方法重复扫上去导致注册tomcat conte ...

  3. 吴裕雄--天生自然python学习笔记:python 用pygame模块动画一让图片动起来

    动画是游戏开发中不可或缺的要素,游戏中的角色只有动起来才会拥有“生命”, 但动画处理也是最让游戏开发者头痛的部分.Pygame 包通过不断重新绘制绘图窗口,短短几行代码就可以让图片动起来! 动画处理程 ...

  4. 电脑C盘空间不足

    电脑C盘空间不足,又不知道哪些文件可以删,可以下载下面的批处理文件 @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.t ...

  5. linux安装源码包报错

    报错代码1如下: [root@xiaoming nginx-]# ./configure --prefix=/soft/nginx- checking for OS + Linux -.el7.x86 ...

  6. Ribbon XML Editor 2019.01.23-Setup.zip(支持64位)

    RibbonXMLEditor是一款用于书写和测试customUI XML代码的国产工具,具有XML验证.Office文档压入.回调函数查询等多种功能. 可作为定制Office自定义界面有关程序开发的 ...

  7. form提供的两种数据传输方式 get和post method=”post“和method=”get”

    虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一些差异,但是了解二者的差异在以后的编程也会很有帮助的. ...

  8. Linux_更改主机名

    老师上linux课截图必须改主机名字,每个人一个代号,所以就写篇这个咯 查看主机名 [root@localhost.localdomain Desktop]# hostname localhost.l ...

  9. 推荐几个树莓派 raspbian 系统 可用的 arm docker源

    树莓派刚到手各种尝鲜,试过不少系统,最后还是回归了raspbian os 系统 root@raspberrypi:/home/pi# cat /proc/version Linux version 4 ...

  10. 安装与使用django-restframework

    django-restframework 一.安装与使用 1.安装 >: pip3 install djangorestframework 2.使用 在settings.py中注册: INSTA ...