scrapy分布式、断点续连爬虫开发框架RedisSpider使用教程

一、爬虫文件

使用RedisSpider为继承父类,添加redis_key

python 复制代码
import scrapy
from ..items import NewsItem
from scrapy_redis import spiders

class CbsnewsSpiderSpider(spiders.RedisSpider):
    name = "abc_spider"
    # allowed_domains = ["www.abc.com"]
    # start_urls = ["https://www.abc.com/"]
    redis_key = 'abc:start_urls'    # redis队列关键字,使用 lpush abc:start_urls https://www.abc.com/ 放入初始网址,程序自动GET请求

    def parse(self, response):
        """
        第一层:解析自动GET请求的初始网址
        :param response:
        :return:
        """
        abc_list = response.xpath('//nav[@class="header__nav"]//a/@href').extract()
        if not abc_list:
            return
        for column_url in abc_list:
            yield scrapy.Request(column_url, callback=self.abc_source, meta={'column_url': column_url})

    def abc_source(self,response):
        """
        第二层:解析第一层获取并请求回来的网址
        :param response:
        :return:
        """
        column_url = response.meta['column_url']
        print(column_url)
        pass

二、settings.py配置

项目settings.py文件增加下面代码即可

python 复制代码
# 增加redis地址、端口
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379

# 配置scrapy-redis调度器
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# 配置爬取去重
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# 断点续连,不用从头开始爬
SCHEDULER_PERSIST = True
相关推荐
liulilittle3 小时前
论无知:分布式
服务器·网络·分布式·并发·通信·竞态
脱胎换骨-军哥5 小时前
C++ 与 Go 生成质量比拼,谁是分布式主力语言
c++·分布式·golang
oh,huoyuyan18 小时前
火车采集器同时运行超多任务运行优化方案
爬虫·火车采集器
珠***格19 小时前
XGF10-Z-4典型接入方式解析:10千伏专变用户380伏多点分布式光伏如何并网?
网络·人工智能·分布式·安全·边缘计算
阿里云云原生1 天前
Agent 不再是“玩具”!AgentScope Java 2.0 GA 发布:构建企业级分布式智能体底座
java·开发语言·分布式·agentscope
时代的狂1 天前
RabbitMQ 面试问答
分布式·面试·rabbitmq
hkNaruto1 天前
【大数据】《传统Hadoop大数据技术入门:补充与进阶——从数据格式到智能决策的问答实录》
大数据·hadoop·分布式
ThanksGive1 天前
TurboLock:从 v1 Redis 分布式锁到 v2 Go 并发控制中间件
分布式·后端
TlSfoward1 天前
爬虫指纹漂移监控与回归测试:JA3/JA4 变化为什么会影响线上验证 TLSFOWARD
数据库·爬虫·网络协议·搜索引擎
Albart5751 天前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python