我是靠谱客的博主 魁梧钢笔,最近开发中收集的这篇文章主要介绍Scrapy存储jsonScrapy存储json,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Scrapy存储json

1.使用终端指令行方式

  • 指令:scrapy crawl xxx -o filePath.文件类型 -s FEED_EXPORT_ENCODING=utf-8
  • -s FEED_EXPORT_ENCODING=utf-8 解决中文乱码
  • 注意:持久化存储对应的文本文件类型只可以为 ‘json’, ‘jsonlines’, ‘jl’, ‘csv’, ‘xml’, ‘marshal’, ‘pickle’ 类型

终端指令方式存储数据格式

2.直接创建并写入 json 文件

import os
import codecs
import json


class LiepinproPipeline(object):
    # 构造方法(初始化对象时执行的方法)
    def __init__(self):
        # 必须使用 w+ 模式打开文件,以便后续进行 读写操作(w+模式,意味既可读,亦可写)
        # 注意:此处打开文件使用的不是 python 的 open 方法,而是 codecs 中的 open 方法
        self.json_file = codecs.open('data2.json', 'w+', encoding='UTF-8')

    # 爬虫开始时执行的方法
    def open_spider(self, spider):
        # 在爬虫开始时,首先写入一个 '[' 符号,构造一个 json 数组
        # 为使得 Json 文件具有更高的易读性,我们辅助输出了 'n'(换行符)
        self.json_file.write('[n')

    # 爬虫 pipeline 接收到 Scrapy 引擎发来的 item 数据时,执行的方法
    def process_item(self, item, spider):
        # 将 item 转换为 字典类型,并编码为 json 字符串,写入文件
        # 为使得 Json 文件具有更高的易读性,辅助输出 'n'(换行符)
        item_json = json.dumps(dict(item), ensure_ascii=False, indent=2)
        self.json_file.write(item_json + ',n')
        return item

    # 爬虫结束时执行的方法
    def close_spider(self, spider):
        # 在结束后,需要对 process_item 最后一次执行输出的 “逗号” 去除
        # 当前文件指针处于文件尾,我们需要首先使用 SEEK 方法,定位文件尾前的两个字符(一个','(逗号), 一个'n'(换行符))的位置
        self.json_file.seek(-2, os.SEEK_END)
        # 使用 truncate() 方法,将后面的数据清空
        self.json_file.truncate()
        # 重新输出'n',并输入']',与 open_spider(self, spider) 时输出的 '[',构成一个完整的数组格式
        self.json_file.write('n]')
        # 关闭文件
        self.json_file.close()

直接写入方式存储数据格式

3.使用 JsonItemExporter 写入 Json 文件

from scrapy.exporters import JsonItemExporter


class LiepinproPipeline(object):

    # 构造方法(初始化对象时执行的方法)
    def __init__(self):
        # 使用 'wb' (二进制写模式)模式打开文件
        self.json_file = open('data3.json', 'wb')
        # 构建 JsonItemExporter 对象,设定不使用 ASCII 编码,并指定编码格式为 'UTF-8'
        self.json_exporter = JsonItemExporter(self.json_file, ensure_ascii=False, encoding='UTF-8', indent=2)
        # 声明 exporting 过程 开始,这一句也可以放在 open_spider() 方法中执行。
        self.json_exporter.start_exporting()

    # 爬虫 pipeline 接收到 Scrapy 引擎发来的 item 数据时,执行的方法
    def process_item(self, item, spider):
        # 将 item 存储到内存中
        self.json_exporter.export_item(item)
        return item

    def close_spider(self, spider):
        # 声明 exporting 过程 结束,结束后,JsonItemExporter 会将收集存放在内存中的所有数据统一写入文件中
        self.json_exporter.finish_exporting()
        # 关闭文件
        self.json_file.close()

JsonItemExporter写入方式存储数据格式

4.使用 JsonLinesItemExporter 写入 json 文件

from scrapy.exporters import JsonLinesItemExporter


class LiepinproPipeline(object):

    # 构造方法(初始化对象时执行的方法)
    def __init__(self):
        # 使用 'wb' (二进制写模式)模式打开文件
        self.json_file = open('data4.json', 'wb')
        # 构建 JsonLinesItemExporter 对象,设定不使用 ASCII 编码,并指定编码格式为 'UTF-8'
        self.json_exporter = JsonLinesItemExporter(self.json_file, ensure_ascii=False, encoding='UTF-8', indent=2)
        # 声明 exporting 过程 开始,这一句也可以放在 open_spider() 方法中执行。
        self.json_exporter.start_exporting()

    # 爬虫 pipeline 接收到 Scrapy 引擎发来的 item 数据时,执行的方法
    def process_item(self, item, spider):
        # 将 item 直接写入文件中
        self.json_exporter.export_item(item)
        return item

    def close_spider(self, spider):
        # 声明 exporting 过程 结束,结束后,JsonItemExporter 会将收集存放在内存中的所有数据统一写入文件中
        self.json_exporter.finish_exporting()
        # 关闭文件
        self.json_file.close()

JsonLinesItemExporter写入方式存储数据格式

四种写入方式对比

方式内存占用情况是否为标准json格式易读性
终端指令行存储较高
直接创建并写入文件
JsonItemExporter
JsonLinesItemExporter

最后

以上就是魁梧钢笔为你收集整理的Scrapy存储jsonScrapy存储json的全部内容,希望文章能够帮你解决Scrapy存储jsonScrapy存储json所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部