Python uses a database connection "Nine"

   In interface testing, the direct use r.json () to get the result, inevitably with the Chinese in the results, but in Chinese the default console output is Unicode encoded, can not be set through a unified utf-8 in the settings, Therefore, in order to show the console more obvious tips Chinese, there is the following operations.

 

Use the Editor: pycharm

Direct printing: print r.json ()

Chinese displayed on the console:

     

solution:

import json
get_result = r.json()
print json.dumps(get_result, encoding="utf-8", ensure_ascii=False)

Print Results:

       

 

Detailed reference code:

#!/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()

 

 

 

 

Guess you like

Origin www.cnblogs.com/syw20170419/p/11005815.html