python - scrapy 使用item export输出中文到json文件,内容为unicode码,如何输出为中文?
阿神
阿神 2017-04-17 12:00:00
[Python讨论组]

问题如题.

阿神
阿神

闭关修行中......

全部回复(3)
阿神

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 就可以啦。

阿神

存储为中为编码的问题可能较多,一般处理编码的问题在下列地方
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

黄舟

可以试试

cat xxx.json | json_pp| less

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号