python requests请求返回结果中文乱码处理

 

用requests请求接口时,偶尔返回值message字段包含中文,如果用json.dumps(response.text),中文会展示乱码.

可以在json.dumps()方法中加一个参数ensure_ascii=False,来避免中文转ascii码,这样中文就正常展示了

如下所示:

def get_issue(issueKey):
    url = "http://xxx/xxx"

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    } 
    client = requests.Session()
    client.keep_alive = True
    client.headers = headers

    res1 = client.get(url)
    print(json.dumps(json.loads(res1.text), sort_keys=True, indent=4, separators=(",", ": "), ensure_ascii=False))

 

结果:

 

 

 

posted @ 2021-06-24 16:54  夏晓旭  阅读(4165)  评论(0编辑  收藏  举报