注意:此方法跟之前保存成json文件的写法有少许不同之处,注意区分

情境再现:

使用scrapy抓取中文网页,得到的数据类型是unicode,在控制台输出的话也是显示unicode,如下所示

{'author': u'\u51af\u53cb\u5170\u7b49',
 'classification': u' \u4eba\u6587\u793e\u79d1',
 'down_bd_code': u'\u63d0\u53d6\u5bc6\u7801\uff1asp6t',
 'down_bd_url': u'https://pan.baidu.com/s/1N1NPVupmnPX6W5Fm2YHccg',
 'title': u'\u4e2d\u897f\u65b9\u54f2\u5b66\u53f2\uff08\u5957\u88c5\u51712\u518c\uff09'}

保存成json文件时需要显示出中文

import json
import codecs # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html class WriteJsonPipeline(object):
def __init__(self):
self.file = codecs.open('items.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()

将以上内容插入pipelines.py,同时在settings.py中加入

ITEM_PIPELINES = {
    'panda.pipelines.WriteJsonPipeline': 300
}

以调用pipelines文件

解决Scrapy抓取中文网页保存为json文件时中文不显示而是显示unicode的问题的更多相关文章

  1. python 数据写入json文件时中文显示Unicode编码问题

    一.问题描述 import json dir = { '春晓':'asfffa', '春眠不觉晓' : '处处闻啼鸟', '夜来风雨声' : 56789, 'asdga':'asdasda' } fp ...

  2. NPOI抓取WPS表格保存的EXCEL文件

    其实是可以抓取的,唯一不同就是Sheet的位置前进了一位.     var sheet1 = (HSSFSheet)hssfworkbook.GetSheetAt(1);     来自为知笔记(Wiz ...

  3. 解决Scrapy抓取中文结果保存为文件时的编码问题

    import json import codecs # Define your item pipelines here # # Don't forget to add your pipeline to ...

  4. scrapy 抓取动态网页

    -- coding: utf-8 -- ''' gouwu.sogou.com Spider, Created on Dec, 2014 version: 1.0 author: chenqx @ht ...

  5. scrapy抓取的中文结果乱码解决办法

    使用scrapy抓取的结果,中文默认是Unicode,无法显示中文. 中文默认是Unicode,如: \u5317\u4eac\u5927\u5b66 在setting文件中设置: FEED_EXPO ...

  6. scrapy抓取中文后乱码解决方法

    出现这种东西不是乱码,是unicode,只是人看不懂,例如: \u96a8\u6642\u66f4\u65b0> \u25a0\u25a0\u25a 我们把他解码成中文码即可,在settings ...

  7. scrapy和selenium结合抓取动态网页

    1.安装python (我用的是2.7版本的) 2.安装scrapy:   详情请参考 http://blog.csdn.net/wukaibo1986/article/details/8167590 ...

  8. 抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法

    抓取https网页时,报错sun.security.validator.ValidatorException: PKIX path building failed 解决办法 原因是https证书问题, ...

  9. scrapy抓取的页面中文会变成unicode字符串

    不了解编码的,需要先补下:http://www.cnblogs.com/jiangtu/p/6245264.html 在学习&使用scrapy抓取网上信息时,发现scrapy 会将含有中文的f ...

随机推荐

  1. Oracle Temp 表空间切换

    一.TEMP表空间作用 暂时表空间主要用途是在数据库进行排序运算.管理索引.訪问视图等操作时提供暂时的运算空间,当运算完毕之后系统会自己主动清理.当 oracle 里须要用到 sort 的时候. PG ...

  2. DM8168 dead JTAG clock

    针对新板调试,不针对EVM板. TI XDS560连上DM8168 20pin仿真接口 launch 8168.ccxml,右击CortexA8,选择Connect Target 出现错误例如以下: ...

  3. CoffeeScript的缩进

    CoffeeScript用缩进代替了花括符"{}",作用范围的划分只靠缩进.这带来代码精简.简洁的同时,有时候也让人困惑. 比如说: if 3 > 1 alert(" ...

  4. 安卓dex 文件结构简要说明

    #ifndef _DEX_FILE_HELPER_ #define _DEX_FILE_HELPER_ //此文件仅仅是起帮助作用,帮助不太了解DexFile结构的了解一下DexFile相关结构,想更 ...

  5. SQLServer添加链接服务器

    右键,添加链接服务器 在安全里面输入用户名和密码 添加成功之后的使用方法 select * from [192.168.1.63,3326].[数据库].[dbo].[表]

  6. A. Power Consumption Calculation

    http://codeforces.com/problemset/problem/10/A 题很简单,就是题意难懂啊... #include <stdio.h> #include < ...

  7. php获得文件的属性

    PHP获取文件属性可以用到多种函数,来实现我们对文件各种不同信息的获取需求.在这里我们就简单的介绍了这些获取方式的实现方法. 详细解读PHP获取远程图片技巧 详细介绍PHP读取目录函数 如何运用相关函 ...

  8. A Few Words on Callbacks and Asynchronous Mechanism In Javascript

    It is said that the one of the most routine work a javascript programmer do is writing codes like &q ...

  9. [Swift通天遁地]二、表格表单-(5)实现表格下拉和上拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. Leetcode03---Longest Substring Without Repeating Characters

    Description: Given a string, find the length of the longest substring without repeating characters. ...