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
相关推荐
国科安芯5 小时前
ASL706S:让 MCU 不再“失忆跑飞“的守护芯片
分布式·单片机·嵌入式硬件·安全·fpga开发·架构
大鹏说大话6 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
阿无,7 小时前
RabbitMQ面试题
分布式·rabbitmq
Minner-Scrapy7 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
盛世宏博智慧档案9 小时前
全国100个分布式机房环境温湿度智能监测系统建设方案
分布式·机房·温湿度
六bring个六10 小时前
分布式设备连接对端失败问题分析
分布式·分布式软总线
陈皮波比茶12 小时前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
不叫猫先生13 小时前
2026 Web Scraping 实战:页面改 class 就归零?用 Bright Data 搭稳定的 Agent 爬虫
爬虫·agent
tang7778913 小时前
Scrapy框架动态IP自动轮换集成配置教程
爬虫·tcp/ip·scrapy·架构·爬虫代理·动态ip
天涯明月199316 小时前
ray深度研究报告
大数据·人工智能·分布式·ray