在接口测试中,直接使用r.json()获取到的结果,难免会在结果中带有中文,但是在控制台的中文输出默认是Unicode编码,不能通过统一在设置中进行设置utf-8,因此为了在控制台更显而易见的显示出中文的提示,就有了如下的操作。
使用的编辑器:pycharm
直接打印:print r.json()
中文在控制台的显示:
解决方案:
import json
get_result = r.json()
print json.dumps(get_result, encoding="utf-8", ensure_ascii=False)
打印结果:
详细的参考代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:lucky,time:2019-06-11
import requests
import json
def test_login():
url = "https://*****.*****.com/v2/user/login" # 接口
url_Write_excel = url[url.rfind('/v2'):] # 获取非域名外的url链接,最后写入到Excel中
data = {
"mobile": "12606666333",
"password": "ee79976c9380d5e337fc1c095ece8c8f22f91f306ceeb161fa51fecede2c4ba1"
}
headers = {
"version": "2.3.1",
"version-id": "239",
"device-id": "8BAFD18C-61E3-4BAF-8AB1-0BC16E567633",
"time": "1560175716224",
"channel-id": "001",
"os": "ios",
"Accept-Language": "zh-tw",
"device-name": "iPhoneX",
"User-Agent": "iBer/239 CFNetwork/902.2 Darwin/17.7.0",
# 注:一定不能加content-type,否则报签名错误
# Content-Type: multipart/form-data; boundary=vHsPJ5s6grMzpmWoZxd3T3o6.LcUWBvUUu0gDNubaf05Ve7kv6bAkH3s_rr0AEc2D6AbEh
"sign": "629bb0721dc60ff71725b40f46a3d1b5"
}
r = requests.post(url=url, data=data, headers=headers, timeout=100)
get_result = r.json()
print json.dumps(get_result, encoding="utf-8", ensure_ascii=False)
test_login()