scrapy 爬虫返回json格式内容unicode编码转换为中文的问题解决

最近在基于python3.6.5 的环境使用scrapy框架爬虫获取json数据,返回的数据是unicode格式的,在spider里面的parse接口中打印response.text出来如下:

class TestSpider(Spider):
......
    def parse(self, response):
	    print(response.text)

结果如下:

{
	"status":"true",
	"last_view_time":null,
	"message":"",
	"shown_offset":0,
	"articles":[
	{
		"channel":"u8d44u8bafnew",
		"comments":113,
		"created_at":"09u670828u65e5",
		"desc":"  u00a0 u00a0 u00a0 u00a0 u00a0u00a0u5173u6ce8ITValueuff0cu67e5u770bu4f01u4e1au7ea7u5e02u573au6700u65b0u9c9cu3001u6700u5177u4ef7u503cu7684u62a5u9053uff01u4e2du56fdu667au6167u529eu516cu54c1u724cu6df1u5733u5e02u84ddu51cc
		.......

python3版本开始取消了string的decode方法,不能像以前一样使用类似mystring.decode(“utf-8”) 的方式转码。

其实可以绕一下解决,先编码再解码:

 def parse(self, response):
     datas = json.dumps(response.text, ensure_ascii= False, indent=4, separators=(',', ': '))
     json_data = json.loads(datas).encode('utf-8').decode('unicode_escape')
     print(json_data)

关键在于:mystr.encode('utf-8').decode('unicode_escape')

最后打印内容正常了:

{
	"status":"true",
	"last_view_time":null,
	"message":"",
	"shown_offset":0,
	"articles":[
	{
		"channel":"默认",
		"comments":25,
		"created_at":"09月28日",
		"desc":"  了解快捷键能够提升您的生产力。这里有一些实用的 Ubuntu 快捷键助您像专业人士一样使用 Ubuntu。-- Abhishek Prakash有用的原文链接请访问文末的...","downs":0,"id":"82879369","isexpert":0,"sourcetype":1,"tag":"","title"
		............
原文地址:https://www.cnblogs.com/xiaocy66/p/10589258.html