数据存储 Json

news/2024/7/19 11:32:11 标签: json, python, 爬虫

数据存储 Json

一、JsonLInesEx

 1 from scrapy.exporters import JsonLinesItemExporter
 2 class JsonLinesItemExporterPipeline(object):
 3     def __init__(self):
 4         self.file = open('jsonfile.json', 'wb')  # 必须写入二进制
 5         self.exporter = JsonLinesItemExporter(self.file, ensure_ascii=False, encoding='utf-8')
 6     def process_item(self, item, spider):
 7         self.exporter.export_item(item)
 8         print(item)
 9     def close_item(self, spider):
10         self.file.close()
11         pass
JsonLinesItemExporter

二、自定义方法保存json文件

 1 import json
 2  
 3 # 自定义处理json保存
 4 class QsbkDemoPipeline(object):
 5     def __init__(self):
 6         self.file = open('qsbk.json', 'w', encoding='utf-8')
 7  
 8     def open_spider(self, spider):
 9         print('爬虫开始了...')
10         pass
11  
12     def process_item(self, item, spider):
13         # 这里需要把item转换字典
14         item_json = json.dumps(dict(item), ensure_ascii=False)
15         self.file.write(item_json+'\n')
16         return item
17  
18     def close_spider(self, spider):
19         self.file.close()
20         print('爬虫结束了...')
21         pass
View Code

三、JsonItemExporter保存json

 1 from scrapy.exporters import JsonItemExporter
 2  
 3 # 利用scrapy自带json保存
 4 class JsonExporterPipeline(object):
 5     def __init__(self):
 6         self.file = open('qsbk_1.json', 'wb')  # 必须二进制写入
 7         self.exporter = JsonItemExporter(self.file, encoding='utf-8', ensure_ascii=False)
 8         # 开始写入
 9         self.exporter.start_exporting()
10  
11     def open_spider(self, spider):
12         print('爬虫开始')
13         pass
14  
15     def process_item(self, item, spider):
16         self.exporter.export_item(item)
17         return item
18  
19     def close_spider(self, spider):
20         # 完成写入
21         self.exporter.finish_exporting()
22         self.file.close()
23         pass
View Code

 

转载于:https://www.cnblogs.com/guozepingboke/p/10794661.html


http://www.niftyadmin.cn/n/904776.html

相关文章

矩阵运算概要

提到矩阵,相信大家都不陌生,作为最基本的数学概念之一,我们 知道常用的矩阵运算包括:矩阵加减乘、行列式、转置矩阵、逆矩阵、特征向量(特征值)。 具体概念不做过多讲讲,直接上代码&#xff08…

Elasticsearch 简介

Elasticsearch 简介转载于:https://www.cnblogs.com/guozepingboke/p/10795427.html

四元数定义三维旋转

四元数(Quaternion)是由爱尔兰数学家 哈密顿(Hamilton)在1843年发明的概念。四元数的乘法不符合交换律(commutative law)。 四元数 可以描述为 (x * i, y * j, z * k, w) ,其中(x,y,z&#x…

F-神殿-2018年第二届河北省大学生程序设计竞赛(位运算)

icebound通过勤工俭学,攒了一小笔钱,于是他决定出国旅游。这天,icebound走进了一个神秘的神殿。神殿由八位守护者守卫,总共由 6464 64个门组成,每一道门后都有一个迷宫,迷宫的大小均为 100100100 \times 10…

多边形Polygon

多边形 大家都很熟悉,我们把他定义为 多条边首尾连接的封闭图形,关于多边形的算法有很多,比如 是否为凸多边形、直线与多边形求交、填充算法、三角化等,具体的算法原理作者就不再细讲,直接给出作者以前写的具体代码&am…

小程序中 radio 的一个坑,到底 checked 该赋什么值?

https://blog.csdn.net/henryhu712/article/details/83059365 转载于:https://www.cnblogs.com/yanyunliu/p/10800753.html

导数与微分

导数的定义: 当函数 yf(x) 的自变量 x 在一点 x0 上产生一个增量 Δx 时,函数输出值的增量 Δy 与自变量增量 Δx 的比值在 Δx 趋于0时的极限 如果存在,即为在 x0 处的导数: 导数公式定义为: 导数的意义: …

通量与散度

一. 通量 通量(flux),从物理上理解,表示物质分子移动量的大小,指某种物质在每秒内通过每平方厘米的假想平面的摩尔或毫尔数。 假设有向量场 A: 沿场中某一有向曲面 ∑ 的第二类曲面积分为: 成为…