Flask返回浏览器无乱码方法

python 复制代码
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, Response
import os
import json
import re
from datetime import datetime
import logging
import sys
import crawling_web_knowledge

app = Flask(__name__)


app.json.ensure_ascii = False  # 解决中文乱码问题


@app.route('/')
def index1():
    return {"code": 200, "message": "success"}


@app.route('/message', methods=['GET', 'POST'])
def index2(type="new"):
    start_time = datetime.now()

    if request.method == 'GET':
        content = request.args.get('content')
    elif request.method == 'POST':
        content = request.form.get('content')

    resdict = 方法
    resdict["code"] = 200
    resdict["input"] = content

    end_time = datetime.now()
    # print("计时结束", end)
    # 计算时长
    duration = end_time - start_time
    print(f"任务执行时长: {duration.total_seconds()}")

    resdict = json.dumps(resdict, ensure_ascii=False)
    # 创建一个Response对象,并设置Content-Type为application/json
    response = Response(resdict, content_type='application/json')
    logging.info("#content:{}###resdict:{}#".format(content, response))

    return response


if __name__ == '__main__':
    logging.basicConfig(stream=sys.stdout, format='%(asctime)s %(levelname)s:%(message)s', level=logging.INFO,
                        datefmt='%I:%M:%S')
    app.run(debug=True, port=XXX, host='0.0.0.0')

1、先json.dumps(对应字典, ensure_ascii=False)编码

2、然后

复制代码
Response(对应字典, content_type='application/json')

方可解决浏览器显示乱码问题

相关推荐
杰瑞哥哥11 小时前
【时间序列与深度学习】(一)经济计量基础ARIMA模型
python·时间序列·金融工程
网安CILLE11 小时前
Wireshark 抓包实战演示
linux·网络·python·测试工具·web安全·网络安全·wireshark
王夏奇11 小时前
python中的基础知识点-1
开发语言·windows·python
叫我辉哥e111 小时前
新手进阶Python:办公看板集成多数据源+ECharts高级可视化
开发语言·python·echarts
程序员敲代码吗11 小时前
如何从Python初学者进阶为专家?
jvm·数据库·python
派大鑫wink11 小时前
【Day39】Spring 核心注解:@Component、@Autowired、@Configuration 等
java·后端·spring
狗都不学爬虫_11 小时前
JS逆向 - 最新版某某安全中心滑块验证(wasm设备指纹)
javascript·爬虫·python·网络爬虫·wasm
阿杰 AJie11 小时前
Lambda 表达式大全
开发语言·windows·python
格鸰爱童话11 小时前
python基础总结
开发语言·python
tjjucheng11 小时前
小程序定制开发哪家有成功案例
python