您的位置:首页 > Web前端 > JavaScript

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:

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] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: