分布式爬虫参数设置

本文详细介绍了如何在Scrapy项目中使用RedisSpider进行爬取,包括设置start_urls、处理初始域名、配置settings.py中的关键参数,如dupefilter、scheduler和item pipelines。同时展示了如何连接Redis服务以实现数据持久化和分布式爬取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、自身的spider.py设置

from scrapy_redis.spiders import RedisSpider


class MySpider(RedisSpider):
    redis_key = 'myspider:start_urls'

    def __init__(self, *args, **kwargs):
        domain = kwargs.pop('domain', '')            #当我们初始化爬虫时,如果定义了初始域名,
                                                     #则采用
        self.allowed_domains = filter(None, domain.split(','))  #对域名进行筛选
        super(MySpider, self).__init__(*args, **kwargs)

二、配置文件settings.py的设置

# Scrapy settings for example project
#
# For simplicity, this file contains only the most important settings by
# default. All the other settings are documented here:
#
#     http://doc.scrapy.org/topics/settings.html
#
SPIDER_MODULES = ['example.spiders']
NEWSPIDER_MODULE = 'example.spiders'

USER_AGENT = 'scrapy-redis (+https://github.com/rolando/scrapy-redis)'

DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"    #是否筛选
SCHEDULER = "scrapy_redis.scheduler.Scheduler"    #调度器
SCHEDULER_PERSIST = True                #断点续传
# SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderPriorityQueue"
# SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderQueue"
# SCHEDULER_QUEUE_CLASS = "scrapy_redis.queue.SpiderStack"

ITEM_PIPELINES = {
    'example.pipelines.ExamplePipeline': 300,
    'scrapy_redis.pipelines.RedisPipeline': 400,            /自动放到redis数据库
}

LOG_LEVEL = 'DEBUG'

# Introduce an artifical delay to make use of parallelism. to speed up the
# crawl.
DOWNLOAD_DELAY = 1

REDIS_URL = 'redis://127.0.0.1:6379'        #设置redis服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值