scrapy 框架在通过pipline写入中文数据时出现了乱码,原因是在于json.dumps在序列化时对中文默认使用的ascii编码,会将中文转义为形如"\u4e2d\u56fd"ascii 编码;

因此在将dict转为str时需要关闭ensure_sacii配置ensure_ascii=False

具体解决代码如下:

import json

class MyprojectPipeline(object):
    def open_spider(self, spider):
        self.file = open('fidding.jl', 'w')

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        # 划重点
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item

happy coding!