scrapy_redis分布式爬虫爬取亚马逊图书

  • 最近在学习分布式爬虫,选取当当图书进行了一次小练习
  • 网址,https://www.amazon.cn/gp/book/all_category/ref=sv_b_0
  • 前期准备
    • 安装 redis 数据库,网上由教程请自行谷歌
    • 安装 Scrapy 和 scrapy-redis
    • pip install scrapy(如果出现问题请自行谷歌解决,需要vc环境)
    • pip install scrapy-redis

抓取流程

  • 抓取分类页面下的 “每个大分类下的小分类下的列表页的图书部分内容”

主要代码

  • settings
# -*- coding: utf-8 -*-# Scrapy settings for amazon project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME = 'amazon'SPIDER_MODULES = ['amazon.spiders']
NEWSPIDER_MODULE = 'amazon.spiders'# redis组件
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
SCHEDULER_PERSIST = TrueITEM_PIPELINES = {'scrapy_redis.pipelines.RedisPipeline': 400,
}REDIS_HOST = "127.0.0.1"
REDIS_PORT = 6379
REDIS_PARAMS = {'password': 'root',
}# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'# Obey robots.txt rules
ROBOTSTXT_OBEY = True# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 0.5
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16# Disable cookies (enabled by default)
#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {#    'amazon.middlewares.AmazonSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {#    'amazon.middlewares.AmazonDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {#    'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {#    'amazon.pipelines.AmazonPipeline': 300,
#}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
  • spiders
# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider
from copy import deepcopyclass BookSpider(RedisSpider):name = 'book'allowed_domains = ['amazon.cn']# start_urls = ['http://amazon.cn/']redis_key = "amazon_book"def parse(self, response):div_list = response.xpath('//div[@id="content"]/div[@class="a-row a-size-base"]')for div in div_list:item = {}item['first_title'] = div.xpath('./div[1]/h5/a/@title').extract_first()td_list = div.xpath('./div[2]//td')for td in td_list:item['second_title'] = td.xpath('./a/@title').extract_first()item['second_url'] = td.xpath('./a/@href').extract_first()if item['second_url']:# 有一个url不完整,所以需要判断一下if "http://www.amazon.cn/" in item['second_url']:yield scrapy.Request(url=item['second_url'],callback=self.parse_book_list,meta={'item': deepcopy(item)})def parse_book_list(self, response):item = response.meta['item']li_list = response.xpath('//div[@id="mainResults"]/ul/li')for li in li_list:item['book_name'] = li.xpath('.//div[@class="a-row a-spacing-small"]/div[1]/a/@title').extract_first()item['book_author'] = li.xpath('.//div[@class="a-row a-spacing-small"]/div[2]/span/text()').extract()item['book_type'] = li.xpath('.//div[@class="a-column a-span7"]/div[@class="a-row a-spacing-none"][1]//text()').extract_first()item['book_price'] = li.xpath('.//div[@class="a-column a-span7"]/div[@class="a-row a-spacing-none"][2]/a//text()').extract_first()print(item)# 翻页next_url = response.xpath('(//a[text()="下一页"]|//a[@title="下一页"])/@href').extract_first()if next_url:next_url = "https://www.amazon.cn" + next_urlyield scrapy.Request(url=next_url,callback=self.parse_book_list,meta={'item': item})
  • 执行

    • Master端输入 lpush amazon_book "https://www.amazon.cn/gp/book/all_category/ref=sv_b_0"
    • Slaver端输入 scrapy crawl book
  • 部分执行结果

小结

  • 亚马逊的网址的html都很有规律性,但是就是这种规律性给xpath提取网页数据带来了一些麻烦。
  • 大部分url都是完整的,但是”Kindle今日特价书“板块的url不是完整的,
  • 感觉使用crawlspider的方法来爬取这个网站可能会更好一些。

scrapy_redis分布式爬虫爬取亚马逊图书相关推荐

  1. python爬虫|爬取亚马逊商品库存数据(Selenium实战)

    前言 很多人把selenium爬虫称之为可视化爬虫,之所以这样认为,主要在于selenium爬虫主要是模拟人的点击操作,而selenium驱动浏览器并进行操作的过程是可以观察到的.换言之,就是你在看着 ...

  2. 爬虫(一):用python爬取亚马逊所有家具种类前100名的商品信息(上)

    目标 亚马逊公司(Amazon),是美国最大的一家网络电子商务公司,位于华盛顿州的西雅图,是网络上最早开始经营电子商务的公司之一,现在已成为全球商品品种最多的网上零售商和全球第二大互联网企业. 本次目 ...

  3. 爬虫(二):用python爬取亚马逊所有家具种类前100名的商品信息(下)

    目标 亚马逊公司(Amazon),是美国最大的一家网络电子商务公司,位于华盛顿州的西雅图,是网络上最早开始经营电子商务的公司之一,现在已成为全球商品品种最多的网上零售商和全球第二大互联网企业. 本次目 ...

  4. JAVA爬取亚马逊的商品信息

    在程序里面输入你想爬取的商品名字,就可以返回这件商品在亚马逊搜索中都所有相关商品的信息,包括名字和价格. 解决了在爬取亚马逊时候,亚马逊可以识别出你的爬虫,并返回503,造成只能爬取几个页面的问题. ...

  5. 爬取亚马逊评论_如何利用插件抓取亚马逊评论和关键词?

    如何抓取亚马逊的商品评价? 原本想给大家介绍使用市面上常见或者付费的爬虫工具,直到我发现了这个Chrome的免费插件 --Instant Data Scraper,当时我差点被感动哭了.比起学编程语言 ...

  6. Python基于BeautifulSoup4库爬取亚马逊网页

    引言 我写的这个爬虫非常的简单,没有什么难的逻辑思维,只是简单运用BeautifulSoup进行爬取,相信初学者都可以看懂,就是代码写的比较乱,因为我也是初学者....可能你没有耐心看完,我会尽量一部 ...

  7. Python实现通过ASIN爬取亚马逊产品评论

    Python实现通过ASIN爬取亚马逊产品评论 一.最近一直在研究爬取亚马逊评论相关的信息,亚马逊的反爬机制还是比较严格的,时不时就封cookie啊封ip啊啥的.而且他们的网页排版相对没有那么规则,所 ...

  8. python爬取网页书籍名称代码_python爬取亚马逊书籍信息代码分享

    我有个需求就是抓取一些简单的书籍信息存储到mysql数据库,例如,封面图片,书名,类型,作者,简历,出版社,语种. 我比较之后,决定在亚马逊来实现我的需求. 我分析网站后发现,亚马逊有个高级搜索的功能 ...

  9. 如何使用代理IP进行数据采集,PHP爬虫抓取亚马逊商品数据

    本文关键词:代理IP,私密代理IP,私密代理IP数据采集 什么是代理?什么情况下会用到代理IP?如何使用代理IP进行数据采集 代理服务器的功能就是代理用户去获取网络信息,之后再把相应的信息反馈给客户. ...

最新文章

  1. Error: Discrete value supplied to continuous scale
  2. 上架过程中遇到的问题
  3. python 3.9特性,Python 3.9 正式版要来了,会有哪些新特性?
  4. hdu 1881 毕业bg
  5. php百度人脸识别做登陆,php调用百度人脸识别接口查询数据库人脸信息实现验证登录功能...
  6. 学习Python编程的最好的几本书
  7. Java 8的默认方法:可以做什么和不能做什么?
  8. mac配置环境变量不生效
  9. python 在线培训费用-参加线上python培训班要多少钱?
  10. Windows编程 第七回 绘图课(上)
  11. 从caffe2 开源的代码中抽取 用于加载已训练神经网络参数,使用CPU进行预测的 部分代码,并运行成功一个预测模型...
  12. c语言 int (*p)[5] 类型分析
  13. c# html正则,c# 使用正则解析html
  14. JAVA面向对象之对象和类
  15. 服务器响应为 5.7.0 dt spm,常见邮箱客户端发送失败的错误代码解析
  16. 机器学习课后题——贝叶斯
  17. 应用程序图标变了的解决方法
  18. CSS中 *{ }、*zoom,各种 * 代表的意思
  19. 高性能MySQL读书摘要(五)创建高性能的索引
  20. vs2015编写基本C

热门文章

  1. javascript 中冒号(:)意思?
  2. Unity HoloLens2 开发笔记(六):使用眼动追踪 追踪物体
  3. ANSVC无功补偿装置的应用--在南京某高等院校中
  4. 1.3.1 手写数字识别之数据处理
  5. AUC(一):AUC与Mann–Whitney U test
  6. 洛谷【P1498】:南蛮图腾(分治算法)
  7. 专升本管理学知识点总结——人力资源管理
  8. 5G工业互联网应用(2)——PLC
  9. “李逵”还是“李鬼”,傻傻分不清楚
  10. 飞秋feiQ恢复以前的配置