python请求中的汉字解码
我正在使用newsapi.org并试图获取中文文章。当我把它们打印出来时,它似乎看不懂汉字。将显示u'\u8fdd\u89c4python请求中的汉字解码,python,python-2.7,python-requests,Python,Python 2.7,Python Requests,我正在使用newsapi.org并试图获取中文文章。当我把它们打印出来时,它似乎看不懂汉字。将显示u'\u8fdd\u89c4 {u'articles': [{u'author': u'chinanews', u'description': u'\u8fdd\u89c4 \u539f\u6599\u836f\u5904\u65b9\u836f\u6d41\u5165\u5e02\u573a\u3000\u3000\u5206\u6790\u5404\u5730\u
{u'articles': [{u'author': u'chinanews',
u'description': u'\u8fdd\u89c4 \u539f\u6599\u836f\u5904\u65b9\u836f\u6d41\u5165\u5e02\u573a\u3000\u3000\u5206\u6790\u5404\u5730\u8b66\u65b9\u516c\u5e03\u7684\u5236\u552e\u6709\u6bd2\u6709\u5bb3\u4fdd\u5065\u54c1\u6848\u60c5\u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u6709\u5bb3\u4fdd\u5065\u54c1\u7684\u751f\u4ea7\u539f\u6750\u6599\u4e3b\u8981\u6709\u4e24\u5927\u6e90\u5934\u3002',
u'publishedAt': u'2018-07-14T20:50:00Z',
u'source': {u'id': None, u'name': u'Chinanews.com'},
我的代码是
import requests
url = ('https://newsapi.org/v2/top-headlines?'
'country=cn&'
'apiKey=16a6cd0345d84e799600cdb9ead6f05d')
response = requests.get(url)
pprint(response.json())
我设法做到了这一点:
# A small part of the first part of the string provided:
text = u'\u8fdd\u89c4 \u539f\u6599\u836f'
print(text)
# Prints 违规 原料药
bytes_var = text.encode('utf-8')
print(bytes_var)
# Prints b'\xe8\xbf\x9d\xe8\xa7\x84 \xe5\x8e\x9f\xe6\x96\x99\xe8\x8d\xaf'
现在,这到底有什么问题?这可能与版本有关;我在win10下运行Python 3.6.5。您使用的是Python 2.7,它使用
unicode
类型来存储unicode字符串(例如,存储在JSON中的字符串)。unicode类型的repr
使用unicode转义(\uxxx
)打印所有非ASCII字符,这就是您在pprint
输出中看到的
您需要单独打印该值以查看字符:
>>> print repr(u'\u8fdd\u89c4 \u539f\u6599\u836f')
u'\u8fdd\u89c4 \u539f\u6599\u836f'
>>> print u'\u8fdd\u89c4 \u539f\u6599\u836f'
违规 原料药
因此,在您的情况下,您可以执行以下操作
for article in response.json()['articles']:
print article['description']