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

scrapy生成json中文乱码解决

2015-09-22 18:31 836 查看
ITEM_PIPELINES = [‘xxx.pipelines.JsonWithEncodingPipeline’]

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting

import json
import codecs
import os

class JsonWithEncodingPipeline(object):

def __init__(self):
self.file = codecs.open('scraped_data_utf8.json', 'w', encoding='utf-8')
self.file.write('[')

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

def close_spider(self, spider):
self.file.seek(-1, os.SEEK_END)
self.file.truncate();
self.file.write(']')
self.file.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scrapy python