1.json文件中文解码:

#!/usr/bin/python
#coding=utf-8
#author=dahu
import json
with open('huxiu.json','r') as f:
data=json.load(f)
print data[0]['title']
for key in data[0]:
print '\"%s\":\"%s\",'%(key,data[0][key])

read_from_json

中文写入json:

#!/usr/bin/python
#coding=utf-8
#author=dahu
import json
data={
"desc":"女友不是你想租想租就能租",
"link":"/article/214877.html",
"title":"押金8000元,共享女友门槛不低啊"
}
with open('tmp.json','w') as f:
json.dump(data,f,ensure_ascii=False) #指定ensure_ascii

write_to_json

2.scrapy在保存json文件时,容易乱码,

例如:

scrapy crawl huxiu --nolog -o huxiu.json
$ head huxiu.json
[
{"title": "\u62bc\u91d18000\u5143\uff0c\u5171\u4eab\u5973\u53cb\u95e8\u69db\u4e0d\u4f4e\u554a", "link": "/article/214877.html", "desc": "\u5973\u53cb\u4e0d\u662f\u4f60\u60f3\u79df\u60f3\u79df\u5c31\u80fd\u79df"},
{"title": "\u5f20\u5634\uff0c\u817e\u8baf\u8981\u5582\u4f60\u5403\u836f\u4e86", "link": "/article/214879.html", "desc": "\u201c\u8033\u65c1\u56de\u8361\u7740Pony\u9a6c\u7684\u6559\u8bf2\uff1a\u597d\u597d\u7528\u8111\u5b50\u60f3\u60f3\uff0c\u4e0d\u5145\u94b1\uff0c\u4f60\u4eec\u4f1a\u53d8\u5f3a\u5417\uff1f\u201d"},

结合上面保存json文件为中文的技巧:

settings.py文件改动:

ITEM_PIPELINES = {
'coolscrapy.pipelines.CoolscrapyPipeline': 300,
}

注释去掉

pipelines.py改成如下:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
# import codecs class CoolscrapyPipeline(object):
# def __init__(self):
# self.file = codecs.open('data_cn.json', 'wb', encoding='utf-8') def process_item(self, item, spider):
# line = json.dumps(dict(item),ensure_ascii=False) + '\n'
# self.file.write(line) with open('data_cn1.json', 'a') as f:
json.dump(dict(item), f, ensure_ascii=False)
f.write(',\n')
return item

注释的部分是另一种写法,核心在于settings里启动pipeline,会自动运行process_item程序,所以就可以保存我们想要的任何格式

此时终端输入命令

scrapy crawl huxiu --nolog

如果仍然加 -o file.json ,file和pipeline里定义文件都会生成,但是file的json格式仍然是乱码。

3.进一步

由上分析可以得出另一个结论,setting里的ITEM_PIPELINES 是控制着pipeline的,如果我们多开启几个呢:

ITEM_PIPELINES = {
'coolscrapy.pipelines.CoolscrapyPipeline': 300,
'coolscrapy.pipelines.CoolscrapyPipeline1': 300,
}
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
# import codecs class CoolscrapyPipeline(object):
# def __init__(self):
# self.file = codecs.open('data_cn.json', 'wb', encoding='utf-8') def process_item(self, item, spider):
# line = json.dumps(dict(item),ensure_ascii=False) + '\n'
# self.file.write(line) with open('data_cn1.json', 'a') as f:
json.dump(dict(item), f, ensure_ascii=False)
f.write(',\n')
return item
class CoolscrapyPipeline1(object): def process_item(self, item, spider):
with open('data_cn2.json', 'a') as f:
json.dump(dict(item), f, ensure_ascii=False)
f.write(',hehe\n')
return item

pipelines.py

运行:

$ scrapy crawl huxiu --nolog
$ head -n  data_cn*
==> data_cn1.json <==
{"title": "押金8000元,共享女友门槛不低啊", "link": "/article/214877.html", "desc": "女友不是你想租想租就能租"},
{"title": "张嘴,腾讯要喂你吃药了", "link": "/article/214879.html", "desc": "“耳旁回荡着Pony马的教诲:好好用脑子想想,不充钱,你们会变强吗?”"}, ==> data_cn2.json <==
{"title": "押金8000元,共享女友门槛不低啊", "link": "/article/214877.html", "desc": "女友不是你想租想租就能租"},hehe
{"title": "张嘴,腾讯要喂你吃药了", "link": "/article/214879.html", "desc": "“耳旁回荡着Pony马的教诲:好好用脑子想想,不充钱,你们会变强吗?”"},hehe

可以看到两个文件都生成了!而且还是按照我们想要的格式!

scrapy中输出中文保存中文的更多相关文章

  1. python3中OpenCV imwrite保存中文路径文件

    原先一段将特征值保存为图片的代码,这部分学生的电脑上运行没有生成图片 代码的基本样子是: import os import cv2 import numpy as np def text_to_pic ...

  2. MyEclipse右键new菜单项的设置 及 Eclipse中各种文件不能保存中文的问题

    有时候,myeclipse右键new的时候经常出现一些ejb等文件你懂的,很是恶心~~ Window --> Customize Perspective --> Submenus --&g ...

  3. iText中输出中文

    原文链接 http://hintcnuie.iteye.com/blog/183690 转载内容 iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体 BaseFont.c ...

  4. iText中输出 中文

    iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体    BaseFont.createFont("STSong-Light", "UniG ...

  5. JAVA- JSP中解决无法在Cookie当中保存中文字符的问题

    因为cookie的值是ASCII字符,不能直接把自定义cookie的值直接赋值为中文,但是要实现这个功能,还是有方法的. 1.java中已经给我们提供了方法,此时只需要导入该包就行 <%@ pa ...

  6. 织梦后台系统设置在PHP5.4环境中不能保存中文参数的解决方法

    在没用PHP5.4的环境做Dede后台的时候,织梦58一直没有遇到这个问题,昨天上传一个新的模版到空间去测试发现后台的系统基本参数设置中所有的中文内容都无法保存,关于这个问题,其实以前也听说过,知识一 ...

  7. scrapy抓取的页面中文会变成unicode字符串

    不了解编码的,需要先补下:http://www.cnblogs.com/jiangtu/p/6245264.html 在学习&使用scrapy抓取网上信息时,发现scrapy 会将含有中文的f ...

  8. EF 连接MySQL 数据库  保存中文数据后乱码问题

    EF 连接MySQL 数据库  保存中文数据后乱码问题 采用Code First 生成的数据库,MySQL数据库中,生成的表的编码格式为***** 发现这个问题后,全部手动改成UTF8(图是另一个表的 ...

  9. 处理SecureCRT中使用vim出现中文乱码问题

    处理SecureCRT中使用vim出现中文乱码问题 引用原文:http://blog.chinaunix.net/uid-20639775-id-3475608.html因为cat没有问题,定位是vi ...

随机推荐

  1. 题解-bzoj4061 CERC-2012Farm and Factory

    Problem Please contact lydsy2012@163.com! 题意概要:给定\(n\)点\(m\)边无向图,设定两个起点为\(1,2\),现要求在图中增加一个点,并将这个点与其他 ...

  2. 【转】浅析SkipList跳跃表原理及代码实现

    SkipList在Leveldb以及lucence中都广为使用,是比较高效的数据结构.由于它的代码以及原理实现的简单性,更为人们所接受.首先看看SkipList的定义,为什么叫跳跃表? "S ...

  3. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  4. python操作三大主流数据库(5)python操作mysql⑤使用Jinja2模板提取优化页面展示

    python操作mysql⑤使用Jinja2模板提取优化页面展示 在templates目录下的index.html.cat.html等页面有一些共同的元素,代码比较冗余可以使用模板提取公共代码,在各网 ...

  5. 时间日期date/cal

    命令: date 作用: 查看下系统时间 使用: date 命令: cal 对应英文: calendar 作用: 查看日历 选项: -y:可查看一年的日历 使用: cal cal -y

  6. Day6------------软连接和硬链接

    一.块 1.操作系统分四大类块 super block 掌管全局 inode block directory block block 2.删文件 普通删除只是删除链接,数据还在硬盘 彻底删除:覆盖操作 ...

  7. 16)django-ajax使用

    通过ajax可以悄悄的把数据传输给服务器,实现页面无刷新. 一:ajax使用语法 1)普通方式 ajax使用语法: $.ajax({ url:"/host", //提交到那里 ty ...

  8. 从外部设置传入Go变量

    前提:必须在build/run时指定 -ldflags="-X main.a=2.0 -X main.b=1" , 且a,b必须是string的变量,不能是常量, 不能是struc ...

  9. 使用Eclipse进行Makefile项目

    最近在MCU on Eclipse网站上看到Erich Styger所写的一篇有关在Eclipse中使用Makefile创建项目的文章,文章讲解清晰明了非常不错,所以呢没人将其翻译过来供各位同仁参考. ...

  10. STM32L476应用开发之八:便携式气体分析仪项目总结

    在本次项目中,我们实现的实际上是2套设备:便携式氧气分析仪以及便携式甲烷分析仪.但这两台仪器实际使用的主控板我们是设计了一套,所以主控板是适合于这两个设备的. 1.硬件设计 便携式气体分析仪的功能比较 ...