python爬虫之scrapy基于管道持久化存储操作

python爬虫之scrapy基于管道持久化存储操作

本文基于python爬虫之基于终端指令的持久化存储python爬虫之数据解析操作而写

scrapy持久化存储

基于管道:

编码流程:

1、数据解析

2、在item类中定义相关属性

3、将解析的数据封装存储到item类型的对象

4、在管道类的process_item中要将接受到的item对象中存储的数据进行持久化存储操作

5、在配置文件中开启管道

实际操作:

1、在items.py中定义item类

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

import scrapy


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

2、在qiushi.py中将数据封装到item类中

复制代码
import scrapy
from qiushiPro.items import QiushiproItem


class QiushiSpider(scrapy.Spider):
    name = "qiushi"
    # allowed_domains = ["www.xxx.com"]
    start_urls = ["https://www.qiushile.com/duanzi/"]
    # def parse(self, response):
    #     #解析:段子标题+段子内容
    #     li_list = response.xpath('//*[@id="ct"]/div[1]/div[2]/ul')
    #     all_data = []
    #     for li in li_list:
    #         #xpath返回的是列表,但是列表元素一定是Selector类型的对象
    #         #extract可以将Selector对象中data参数存储的字符串提取出来
    #         # title = li.xpath('./li/div[2]/div[1]/a/text()')[0].extract()
    #         title = li.xpath('./li/div[2]/div[1]/a/text()').extract_first()
    #         #列表调用了extract之后,则表示将列表中每一个Selector对象中data对应的字符串提取了出来
    #         content = li.xpath('./li/div[2]/div[2]//text()')[0].extract()
    #
    #         dic = {
    #             'title':title,
    #             'content':content
    #         }
    #         all_data.append(dic)
    #         # print(title,content)

    def parse(self, response):
        #解析:段子标题+段子内容
        li_list = response.xpath('//*[@id="ct"]/div[1]/div[2]/ul')
        all_data = []
        for li in li_list:
            #xpath返回的是列表,但是列表元素一定是Selector类型的对象
            #extract可以将Selector对象中data参数存储的字符串提取出来
            # title = li.xpath('./li/div[2]/div[1]/a/text()')[0].extract()
            title = li.xpath('./li/div[2]/div[1]/a/text()').extract_first()
            #列表调用了extract之后,则表示将列表中每一个Selector对象中data对应的字符串提取了出来
            content = li.xpath('./li/div[2]/div[2]//text()')[0].extract()

            item = QiushiproItem()
            item['title'] = title
            item['content'] = content

            yield item#将item提交给了管道

3、在pipelines.py中的process_item类中进行持久化存储

复制代码
# 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 QiushiproPipeline:
    fp = None
    #重写父类的一个方法:该方法只在开始爬虫的时候被调用一次
    def open_spider(self,spider):
        print('开始爬虫......')
        self.fp = open('./qiushi.txt','w',encoding='utf-8')

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

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

        return item
    def close_spider(self,spider):
        print('结束爬虫!')
        self.fp.close()

4、在settings.py配置文件中取消管道注释,开启管道

复制代码
ITEM_PIPELINES = {
   "qiushiPro.pipelines.QiushiproPipeline": 300,
    #300表示的是优先级,数值越小优先级越高
}

运行:终端输入scrapy crawl qiushi可观察到qiushi.txt文件的生成

相关推荐
乘乘凉2 小时前
Python中函数的闭包和装饰器
前端·数据库·python
爱隐身的官人7 小时前
爬虫基础学习-爬取网页项目(二)
前端·爬虫·python·学习
刘恒1234567899 小时前
Pycharm
ide·python·pycharm
今***b11 小时前
Python 操作 PPT 文件:从新手到高手的实战指南
java·python·powerpoint
程序员杰哥12 小时前
Jmeter+Jenkins接口压力测试持续集成
自动化测试·软件测试·python·测试工具·jmeter·jenkins·压力测试
Java开发-楠木12 小时前
【猿人学】web第一届 第13题 入门级 cookie
爬虫·python
司徒轩宇13 小时前
Python secrets模块:安全随机数生成的最佳实践
运维·python·安全
用户7851278147014 小时前
源代码接入 1688 接口的详细指南
python
vortex514 小时前
Python包管理与安装机制详解
linux·python·pip
辣椒http_出海辣椒15 小时前
如何使用python 抓取Google搜索数据
python