Python发送Http请求时,提交中文或者符号中文编码问题的解决方法
2023-09-27 14:27:48 时间
前言
博主最近在用python3比较强大的Django开发web的时候,发现一些url的编码问题,在浏览器提交请求api时,如果url中包含汉子,就会被自动编码掉。呈现的结果是 ==> %xx%xx%xx。如果出现3个百分号为一个原字符则为utf8编码,如果2个百分号则为gb2312编码。下面为大家演示编码和解码的代码。
编码
from urllib.parse import quote
text = quote(text, 'utf-8')
注:text为要进行编码的字符串
解码
from urllib.parse import unquote
text = unquote(text, 'utf-8')
源码
def unquote(string, encoding='utf-8', errors='replace'):
"""Replace %xx escapes by their single-character equivalent. The optional
encoding and errors parameters specify how to decode percent-encoded
sequences into Unicode characters, as accepted by the bytes.decode()
method.
By default, percent-encoded sequences are decoded with UTF-8, and invalid
sequences are replaced by a placeholder character.
unquote('abc%20def') -> 'abc def'.
"""
if '%' not in string:
string.split
return string
if encoding is None:
encoding = 'utf-8'
if errors is None:
errors = 'replace'
bits = _asciire.split(string)
res = [bits[0]]
append = res.append
for i in range(1, len(bits), 2):
append(unquote_to_bytes(bits[i]).decode(encoding, errors))
append(bits[i + 1])
return ''.join(res)
def quote(string, safe='/', encoding=None, errors=None):
"""quote('abc def') -> 'abc%20def'
Each part of a URL, e.g. the path info, the query, etc., has a
different set of reserved characters that must be quoted.
RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists
the following reserved characters.
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
"$" | ","
Each of these characters is reserved in some component of a URL,
but not necessarily in all of them.
By default, the quote function is intended for quoting the path
section of a URL. Thus, it will not encode '/'. This character
is reserved, but in typical usage the quote function is being
called on a path where the existing slash characters are used as
reserved characters.
string and safe may be either str or bytes objects. encoding and errors
must not be specified if string is a bytes object.
The optional encoding and errors parameters specify how to deal with
non-ASCII characters, as accepted by the str.encode method.
By default, encoding='utf-8' (characters are encoded with UTF-8), and
errors='strict' (unsupported characters raise a UnicodeEncodeError).
"""
if isinstance(string, str):
if not string:
return string
if encoding is None:
encoding = 'utf-8'
if errors is None:
errors = 'strict'
string = string.encode(encoding, errors)
else:
if encoding is not None:
raise TypeError("quote() doesn't support 'encoding' for bytes")
if errors is not None:
raise TypeError("quote() doesn't support 'errors' for bytes")
return quote_from_bytes(string, safe)
相关文章
- python matplotlib 中文显示乱码设置
- 飘逸的python - 实现一个pretty函数美丽的输出嵌套字典
- Python mapfinance库②中文标题乱码 与 显示负数 解决方案
- python对列表list里面的元素进行排序
- python Flask 学前班
- Python 包含u字符串转中文(u00)
- 使用python将中文与Unicode编码相互转换
- 关于Python自动化的就业真相
- python os模块技巧
- python -m json.tool中文乱码
- Python方法的重写
- 【Python】xpinyin/Pinyin+中文与拼音相互转换(含音调/声调)/生成中文谐音
- 推荐Python、Django中文文档地址
- 《像计算机科学家一样思考Python(第2版)》——1.7 调试
- python ndarray相关操作:切分
- 基于Python的深度神经网络的中文期刊分类系统
- How to 'apt-get install python-opengl' on Ubuntu22.04
- Python模块 实现过渡性模块重载
- Python爬虫之lxml&&BeautifulSoup库基本使用
- Python学习---JSON补充内容[中文编码 + dumps解析]
- 手把手讲解超详细python入门游戏项目‘打外星飞船’(一)
- python mssql数据库考至excel表格中
- 小学生Python编程 ----- 打鸭子
- python第二十九课——文件读写(读取读取中文字符)
- python基础之range()函数和random模块
- Python赋值运算符
- Python环境搭建及中文编码
- Python中利用plt显示中文标题解决方案
- Python算法:决策树分类