爬去当当书籍信息

多台机器同时爬取,共用一个redis记录 scrapy_redis

带爬取的request对象储存在redis中,每台机器读取request对象并删除记录,经行爬取。实现分布式爬虫

import scrapy
from scrapy_redis.spiders import RedisSpider
from copy import deepcopy class DangdangSpider(RedisSpider):
name = 'dangdang'
allowed_domains = ['dangdang.com']
# 开始爬虫,会从redis的key中读取start_url.
redis_key = "dangdang" # lpush dangdang 'http://book.dangdang.com/' def parse(self, response):
# 大分类
div_list = response.xpath("//div[@class='con flq_body']/div")[:-4]
print(len(div_list), 'duoshao')
for div in div_list:
item = {}
item['b_cate'] = div.xpath("./dl/dt//text()").extract()
item['b_cate'] = [i.strip() for i in item['b_cate'] if len(i.strip())>0] # 过滤掉空字符
print('b_cate:', item['b_cate'])
# 中间分类
if item['b_cate'] == ['创意文具']:
print(item['b_cate'], "pass......")
item['m_cate'] = None
item['s_cate_url'] = div.xpath("./dl/dt/a/@ddt-src").extract_first()
print('s_cate_url:', item['m_cate'])
# yield scrapy.Request(
# item['s_cate_url'],
# callback=self.parse_special,
# meta={'item': deepcopy(item)}
# )
else:
dl_list = div.xpath(".//dl[@class='inner_dl']")
for dl in dl_list:
item['m_cate'] = dl.xpath("./dt//text()").extract()
item['m_cate'] = [i.strip() for i in item['m_cate'] if len(i.strip())>0]
# 小分类
dd_list = dl.xpath("./dd")
for dd in dd_list:
item['s_cate'] = dd.xpath("./a/@title").extract_first()
item['s_cate_url'] = dd.xpath("./a/@ddt-src").extract_first()
# 小分类的所有书籍
if item['s_cate_url'] is not None:
yield scrapy.Request(
item['s_cate_url'],
callback=self.parse_books,
meta={'item': deepcopy(item)}
) def parse_special(self, response):
''' 文具信息 '''
pass def parse_books(self, response):
item = response.meta['item']
# 当前小分类的书籍
li_list = response.xpath("//ul[@class='list_aa ']/li")
if li_list is not None:
for li in li_list:
try:
item['book_price'] = li.xpath(".//span[@class='num']/text()").extract_first() + \
li.xpath(".//span[@class='tail']/text()").extract_first()
except:
item['book_price'] = 'Unknown'
item['book_url'] = li.xpath("./a/@href").extract_first()
if item['book_url'] is not None:
yield scrapy.Request(
item['book_url'],
callback=self.parse_book_detail,
meta={'item': deepcopy(item)}
) def parse_book_detail(self, response):
item = response.meta['item']
item['book_name'] = response.xpath("//div[@class='name_info']/h1/img/text()").extract_first()
item['book_desc'] = response.xpath("//span[@class='head_title_name']/text()").extract_first()
# 这一本书籍的详细信息
span_list = response.xpath("//div[@class='messbox_info']/span")
item['book_author'] = span_list.xpath("./span[1]/a/text()").extract() # 可能多个作者
item['publisher'] = span_list.xpath("./span[2]/a/text()").extract_first()
item['pub_date'] = span_list.xpath("./span[3]/text()").extract_first()
print(item)
# yield item

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

  1. 第三百五十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy分布式爬虫要点

    第三百五十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy分布式爬虫要点 1.分布式爬虫原理 2.分布式爬虫优点 3.分布式爬虫需要解决的问题

  2. scrapy分布式爬虫scrapy_redis二篇

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

  3. scrapy分布式爬虫scrapy_redis一篇

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

  4. Scrapy分布式爬虫,分布式队列和布隆过滤器,一分钟搞定?

    使用Scrapy开发一个分布式爬虫?你知道最快的方法是什么吗?一分钟真的能 开发好或者修改出 一个分布式爬虫吗? 话不多说,先让我们看看怎么实践,再详细聊聊细节~ 快速上手 Step 0: 首先安装 ...

  5. 三十五 Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy分布式爬虫要点

    1.分布式爬虫原理 2.分布式爬虫优点 3.分布式爬虫需要解决的问题

  6. Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章

    二.伯乐在线爬取所有文章 1. 初始化文件目录 基础环境 python 3.6.5 JetBrains PyCharm 2018.1 mysql+navicat 为了便于日后的部署:我们开发使用了虚拟 ...

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

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

  8. Scrapy框架之基于RedisSpider实现的分布式爬虫

    需求:爬取的是基于文字的网易新闻数据(国内.国际.军事.航空). 基于Scrapy框架代码实现数据爬取后,再将当前项目修改为基于RedisSpider的分布式爬虫形式. 一.基于Scrapy框架数据爬 ...

  9. 爬虫--Scrapy-基于RedisSpider实现的分布式爬虫

    爬取网易新闻 需求:爬取的是基于文字的新闻数据(国内,国际,军事,航空) 先编写基于scrapycrawl 先创建工程 scrapy startproject 58Pro cd 58Pro 新建一个爬 ...

随机推荐

  1. zabbix添加自定义监控(自动发现)遇到的问题

    问题:zabbix添加自动发现端口,提示Value should be a JSON object [root@localhost zabbix_agentd.d]# zabbix_get -s 19 ...

  2. google mock C++单元测试框架

    转:google mock C++单元测试框架 2012-03-12 09:33:59 http://blog.chinaunix.net/uid-25748718-id-3129590.html G ...

  3. pfSense QoS IDS

    pfSense QoS IDS 来源 https://blanboom.org/2018/pfsense-setup/ 之前我使用的无线路由器是 RT1900ac,其内置了 QoS 和 IDS/IPS ...

  4. webpack资源加载常用配置

    const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundl ...

  5. css中哪些属性会脱离文档流

    一共有三个属性会使元素脱离文档流: (1)浮动 (2)绝对定位 (3)固定定位 元素脱离文档流会导致父元素高度塌陷,要修复高度塌陷问题,可以将子元素与父元素放在同一个BFC中

  6. 前端编译原理 parser.js源码解读

    前面已经介绍了一个jison的使用,在正常开发中其实已经够用下,下面主要是看了下parser.js代码解读下,作为一些了解. 下面以最简单的文法产生的parser做一些代码注释 下面是一些注释,标示了 ...

  7. Java 之 字节缓冲流

    一.字节缓冲输出流 java.io.BufferedOutputStream extends OutputStream BufferedOutputStream:字节缓冲输出流. 继承自父类的共性成员 ...

  8. 利用 CAKeyframeAnimation实现任意轨迹移动

      自定义 View,实现以下方法即可 - (void)drawRect:(CGRect)rect { // Drawing code // 初始化UIBezierPath UIBezierPath ...

  9. Eclipse上安装websphere

    Eclipse上安装websphere 参考:https://blog.csdn.net/qq_26264237/article/details/90107508 安装websphere插件 WebS ...

  10. python之csv操作

    在使用python爬虫时或者其他情况,都会用到csv存储与读取的相关操作,我们在这里就浅谈一下: CSV(Comma-Separated Values)逗号分隔符,也就是每条记录中的值与值之间是用分号 ...