Python爬虫【基于scrapy对爬取的数据进行多方式存储】

直入主题,这里有一个要求:将爬取到的数据分两个方式存储。我们这里为了演示方便就选择一个是打印到终端,一个是写入txt文件。

注意:这里是基于CrawlSpider的。以下所有关于scrapy的相关知识请见拙作 "Python爬虫之明星框架scrapy的基础使用"  。

直接上代码

Crawl.py

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from CrawlDemo.items import CrawldemoItem


class CrawlSpider(CrawlSpider):
    name = "Crawl"
    # allowed_domains = ["www.xxx.com"]
    start_urls = ["https://movie.douban.com/top250"]

    # 链接提取器:根据指定规则(allow="正则")进行指定链接的提取
    link = LinkExtractor(allow=r"start=\d+&filter=")

    rules = (
        Rule(link, callback="parse_item", follow=True),
    )

    def parse_item(self, response):
        li_list = response.xpath('//*[@id="content"]/div/div[1]/ol/li')

        for li in li_list:
            name = li.xpath('./div/div[2]/div[1]/a/span[1]/text()').extract_first()

            item = CrawldemoItem()
            item['title'] = name

            yield item

items.py

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class CrawldemoItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()
    # pass

pipelines.py

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class CrawldemoPipeline:
    # 专门用来处理item类型对象,可以接收爬虫文件提交过来的item对象,每接收到一个item就会被调用一次
    def process_item(self, item, spider):
        title = item['title']

        print(title)
        print("=" * 30)

        return item


class DemoPipeline:
    # 文件对象
    fp = None

    # 重写父类的一个方法,该方法只在开始爬虫的时候被调用一次
    def open_spider(self, spider):
        self.fp = open('./film.txt', 'w', encoding='utf-8')

    # 专门用来处理item类型对象,可以接收爬虫文件提交过来的item对象,每接收到一个item就会被调用一次
    def process_item(self, item, spider):
        title = item['title']

        self.fp.write(title + '\n')

        return item

    # 重写父类的一个方法,该方法只在结束爬虫的时候被调用一次
    def close_spider(self, spider):
        print("文件存储结束...")
        self.fp.close()

settings.py

配置文件中的一些老生常谈的改动这里就不展示了。

b107501346194303b92ec57ceeebb980.png

这个案例是有效的,是对豆瓣top250的电影名称爬取。

对于多方式的存储,其实际上是去创建多个管道。我们知道,管道文件中一个管道类对应的是将数据存储到一种平台;爬虫文件提交的item只会给管道文件中第一个被执行的管道类接受;process_item中的return item表示将item传递给下一个即将被执行的管道类。

故有了上面这些知识,再加上scrapy的相关知识看懂上面的代码就不是问题了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还有糕手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值