scrapy抓取到中文,保存到json文件为unicode,如何解决.
2015-09-25 23:11
836 查看
http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/overview.html
以上链接是很好的scrapy学些资料.感谢marchtea的翻译.
在学习过程中,碰到一个很棘手的问题: 中文的显示和存储. (中文在控制台显示的为\u77e5\u540d...这样的字符,保存到文件也是这样的)
在网上找了很久,下面这个链接应是最切题的.
http://stackoverflow.com/questions/9181214/scrapy-text-encoding
摘抄如下:
pipelines.py:
按照上面的方法,输出到文件就是正常的中文了.
我的测试代码
以上链接是很好的scrapy学些资料.感谢marchtea的翻译.
在学习过程中,碰到一个很棘手的问题: 中文的显示和存储. (中文在控制台显示的为\u77e5\u540d...这样的字符,保存到文件也是这样的)
在网上找了很久,下面这个链接应是最切题的.
http://stackoverflow.com/questions/9181214/scrapy-text-encoding
摘抄如下:
pipelines.py:
import json import codecs class JsonWithEncodingPipeline(object): def __init__(self): self.file = codecs.open('scraped_data_utf8.json', 'w', encoding='utf-8') def process_item(self, item, spider): line = json.dumps(dict(item), ensure_ascii=False) + "\n" self.file.write(line) return item def spider_closed(self, spider): self.file.close()
按照上面的方法,输出到文件就是正常的中文了.
我的测试代码
搜索关键字和链接: JsonItemExporter ensure_ascii=False JsonItemExporter uxxx python输出json文件\uxxx如何转换成中文 Decode and Encode in Python [ http://yangpengg.github.io/blog/2012/12/13/decode-and-encode-in-python/ ] -- python print输出的是中文但是输出到文件的是\uxxx http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html Scrapy : storing the data http://stackoverflow.com/questions/14073442/scrapy-storing-the-data scrapy 使用item export输出中文到json文件,内容为unicode码,如何输出为中文? http://www.lefern.com/question/15837/scrapy-shi-yong-item-exportshu-chu-zhong-wen-dao-jsonwen-jian-nei-rong-wei-unicodema-ru-he-shu-chu-wei-zhong-wen/ how to put in json utf-8 symbols, not their codes? https://groups.google.com/forum/#!msg/scrapy-users/rJcfSFVZ3O4/ZYsD7CMoCKMJ scrapy text encoding http://stackoverflow.com/questions/9181214/scrapy-text-encoding[/code]
相关文章推荐
- (原)用pixi.js 实现 方块阵点击后原地自转效果
- Javascript 矢量绘图 Sketch.js
- avaScript高级程序设计之在 HTML 中使用 JavaScript 第2.2讲笔记
- avaScript高级程序设计之在 HTML 中使用 JavaScript 第2.1讲笔记
- javascript用户密码加密,js密码加密
- html2canvas js截图并下载
- 原生js实现Ajax
- js url encode
- js操作cookie
- 细说ES7 JavaScript Decorators
- 在网页中JS函数自动执行常用三种方法
- js的数据处理记录
- javascript中 addloadevent和insertAfter函数的使用说明
- 【JavaScript】【学习】对象的创建和继承
- 一个json字符串
- 【前端学习]】javascript数组(Array)
- js倒计时(可定义截止日期)
- rapidjson常见使用示例
- FastJSON解析包含多个自定义类的JSON的方法
- js实现图片上传预览