Requests09--使用text查看返回文本
r.text
r.text查看json响应结果
非utf8编码格式的中文会显示为\u4f60的形式
\u:unicode码,一般其后跟 4个16进制数字
json只支持utf-8编码
使用r.content.decode('unicode-escape')可将unicode数据转为可显示字符
指定字符集如utf-8、gb2312等无法解决
使用r.text查看json中的中文字符
"""
使用r.text查看json中的中文字符
接口需求
接口地址:http://192.168.175.128/interface/4/
方法:get
参数:无
返回值:json类型文本
"""
import requests
url = 'http://192.168.175.128/interface/4/'
r = requests.get(url)
# r.encoding = r.apparent_encoding 主要解决\xe4形式问题
print(r.text)
print(r.content.decode('unicode-escape')) # 解决\u 后面四个数字 \ueb23形式
小结
1)json中的汉字使用的是utf-8的编码,这是固定的
2)json类型的响应如果想用text或content显示中文,要用r.content.decode('unicode-escape')
使用r.text查看json中的中文字符
"""
使用r.text查看json中的中文字符
接口需求
接口地址:http://192.168.139.129:8000/sign/get_event_list/
方法:get
参数:
eid:发布会id,数据库中叫id,接口参数叫eid
返回值:json类型
"""
import requests
url = 'http://192.168.139.129:8000/sign/get_event_list/'
r = requests.get(url,params = {'eid':1})
print(r.text)
print(r.content.decode('unicode-escape')) #显示中文汉字
print(r.json())
本文来自博客园,作者:暄总-tester,转载请注明原文链接:https://www.cnblogs.com/sean-test/p/15508281.html

浙公网安备 33010602011771号