Two methods, one is treated to separate out the Chinese, then string concatenation; the other is a direct link to be modified.
The first method, separate out the Chinese
# -*- coding:utf-8 -*-
from urllib.parse import quote
url = 'http://www.example.com/api.php?text=中文在这里'
x = '中文在这里'
x = quote(x)
print(x)
y = 'http://www.example.com/api.php?text='
print(y + x)
Results are as follows
%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C
http://www.example.com/api.php?text=%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C
The second method, direct links are processed
# -*- coding:utf-8 -*-
from urllib.parse import quote
url = 'http://www.example.com/api.php?text=中文在这里'
# 不带附加参数
print('\n不带附加参数:\n%s' % quote(url))
# 附带不转换字符参数
print('\n附加不转换字符参数:\n%s' % quote(url, safe='/:?=&'))
Results are as follows
不带附加参数:
http%3A//www.example.com/api.php%3Ftext%3D%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C
附加不转换字符参数:
http://www.example.com/api.php?text=%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C
quote available parameters are as follows:
quote(string, safe='/', encoding=None, errors=None)
Wherein the safe range of parameters available
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
Reference website: https: //www.zhihu.com/question/22899135