首页 > scrapy 使用item export输出中文到json文件,内容为unicode码,如何输出为中文?

scrapy 使用item export输出中文到json文件,内容为unicode码,如何输出为中文?

问题如题.


可以试试

cat xxx.json | json_pp| less


存储为中为编码的问题可能较多,一般处理编码的问题在下列地方
json要实现对文件的encode
下载的时候吸入数据到item时要decode
最关键的是在解析为后获得为列表数据时,还不能对列表decode
详细中文编码问题可以参考博客
http://www.addbook.cn/blog/scrapy%E4%B8%AD%E6%96%87%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98


http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence 里面有提到,将 JSONEncoder 的 ensure_ascii 参数设为 False 即可。

而 scrapy 的 item export 文档里有提到

The additional constructor arguments are passed to the
BaseItemExporter constructor, and the leftover arguments to the
JSONEncoder constructor, so you can use any JSONEncoder constructor
argument to customize this exporter.

因此就在调用 scrapy.contrib.exporter.JsonItemExporter 的时候额外指定 ensure_ascii=False 就可以啦。

【热门文章】
【热门文章】