【前言】
本篇是为了记录一次解码出现乱码的解决方案,篇幅较短,废话不多说,请食用
【问题】后端针对一个字符串进行unicode编码后的,前端解码后出现乱码问题
unicode编码后的字符串,直接交给前端解码,会出现乱码的
"Status Code: 200\nHeaders: {'Server': 'nginx', 'Date': 'Fri, 23 Aug 2024 07:35:42 GMT', 'Content-Type': 'text/html', 'Content-Length': '902', 'Last-Modified': 'Thu, 22 Aug 2024 03:20:02 GMT', 'Connection': 'keep-alive', 'ETag': '\"66c6ae62-386\"', 'Accept-Ranges': 'bytes'}\nBody: <!doctype html>\n<html>\n<head>\n <meta charset=\"utf-8\">\n <title>\u00e6\u00a3\u008b\u00e7\u0089\u008c\u00e7\u008e\u00a9\u00e6\u00b3\u0095\u00ef\u00bc\u0081</title>\n <style>\n .container {\n width: 60%;\n margin: 10% auto 0;\n background-color: #f0f0f0;\n padding: 2% 5%;\n border-radius: 10px\n }\n\n ul {\n padding-left: 20px;\n }\n\n ul li {\n line-height: 2.3\n }\n\n a {\n color: #20a53a\n }\n </style>\n</head>\n<body>\n <div class=\"container\">\n <h1>\u00e6\u0081\u00ad\u00e5\u0096\u009c, \u00e7\u00ab\u0099\u00e7\u0082\u00b9\u00e5\u0088\u009b\u00e5\u00bb\u00ba\u00e6\u0088\u0090\u00e5\u008a\u009f\u00ef\u00bc\u0081</h1>\n <h3>\u00e8\u00bf\u0099\u00e6\u0098\u00af\u00e9\u00bb\u0098\u00e8\u00ae\u00a4index.html\u00ef\u00bc\u008c\u00e6\u009c\u00ac\u00e9\u00a1\u00b5\u00e9\u009d\u00a2\u00e7\u0094\u00b1\u00e7\u00b3\u00bb\u00e7\u00bb\u009f\u00e8\u0087\u00aa\u00e5\u008a\u00a8\u00e7\u0094\u009f\u00e6\u0088\u0090</h3>\n <ul>\n <li>\u00e6\u009c\u00ac\u00e9\u00a1\u00b5\u00e9\u009d\u00a2\u00e5\u009c\u00a8FTP\u00e6\u00a0\u00b9\u00e7\u009b\u00ae\u00e5\u00bd\u0095\u00e4\u00b8\u008b\u00e7\u009a\u0084index.html</li>\n <li>\u00e6\u0082\u00a8\u00e5\u008f\u00af\u00e4\u00bb\u00a5\u00e4\u00bf\u00ae\u00e6\u0094\u00b9\u00e3\u0080\u0081\u00e5\u0088\u00a0\u00e9\u0099\u00a4\u00e6\u0088\u0096\u00e8\u00a6\u0086\u00e7\u009b\u0096\u00e6\u009c\u00ac\u00e9\u00a1\u00b5\u00e9\u009d\u00a2</li>\n <li>FTP\u00e7\u009b\u00b8\u00e5\u0085\u00b3\u00e4\u00bf\u00a1\u00e6\u0081\u00af\u00ef\u00bc\u008c\u00e8\u00af\u00b7\u00e5\u0088\u00b0\u00e2\u0080\u009c\u00e9\u009d\u00a2\u00e6\u009d\u00bf\u00e7\u00b3\u00bb\u00e7\u00bb\u009f\u00e5\u0090\u008e\u00e5\u008f\u00b0 > FTP\u00e2\u0080\u009d \u00e6\u009f\u00a5\u00e7\u009c\u008b</li>\n </ul>\n </div>\n</body>\n</html>"
后端需要做一些处理,记得做好异常抛出处理
try:
# 替换转义字符
responseStr = item['response_body'].encode('latin1').decode('unicode_escape')
# 再进行 utf-8 解码
item['response_body'] = responseStr.encode('latin1').decode('utf-8')
except Exception as e:
pass
解码后,就直接传给前端展示