在 settings.py 文件的最后增加如下内容:
描述
一台电脑作为安装 redis,能够被远端访问,可以用 ubuntu 系统其它电脑部署 scrapy-redis 来进行分布式抓取同
一个网站爬虫运行时会把提取到的 url 封装成 request 放到 redis 中的 “dmoz:requests”
从该数据库中提取 request 后下载网页,再把网页的内容发送回 redis 中“dmoz:items”
redis 中的“dmoz:requests”数据库为空,爬取结束
reids 中“dmoz:dupefilter”用来存储抓取过的 url 的指纹(使用哈希函数将 url 运算后的结果),是防止重复抓取的
dmoz:request:待爬取 request 对象
dmoz:items 爬取保存的条目
dmoz:dupefilter:抓取过的 url 的指纹
注:dmoz:爬虫的名称
# -*- coding: utf-8 -*-
# Scrapy settings for douban 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.html
BOT_NAME = 'douban'
SPIDER_MODULES = ['douban.spiders']
NEWSPIDER_MODULE = 'douban.spiders'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
# 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 = {
'douban.middlewares.DoubanSpiderMiddleware': 432,
}
# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
# 'douban.middlewares.UserAgentMiddleware':333,
# 'douban.middlewares.DoubanDownloaderMiddleware': 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 = {
# 'douban.pipelines.DoubanPipeline': 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'
# 分布式设置
REDIS_HOST = "127.0.0.1" # 主机名
REDIS_PORT = 6379 # 端口号
# USER_AGENT 设置
USER_AGENT = 'scrapy-redis (+https://github.com/rolando/scrapy-redis)'
# PIPELINES 存储设置
ITEM_PIPELINES = {
# 'example.pipelines.ExamplePipeline': 300,
'scrapy_redis.pipelines.RedisPipeline': 400,
# 'douban.pipelines.DoubanPipeline': 300,
}
# 去重组件设置
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# 调度器组件设置
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 持续化存储设置
SCHEDULER_PERSIST = True
# Log 设置
LOG_LEVEL = 'DEBUG'
# 延迟
DOWNLOAD_DELAY = 1
# 编码
REDIS_ENCODING = "utf-8"
# redis 编码类型默认:'utf-8'
# 说明: # REDIS_PARAMS 连接参数默认:
REDIS_PARAMS = {'socket_timeout':
30, 'socket_connect_timeout': 30, 'retry_on_timeout': True, 'encoding':REDIS_ENCODING,
}
爬虫方法名不能用parse
数据存储
items 数据直接存储在 Redis 数据库中,这个功能已经由scrapy-redis 自行实现。除非单独做额外处理(比如直接存入本地数据库等),否则不用编写 pipelines.py 代码。