python中URL转换会使用urlencode,一般用法如下所示:
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 | from urllib.parse import urlencode params = {"value": "english", "page": 1, "desc": "英语"} print(urlencode(params)) #输出:value=english&page=1&desc=%E8%8B%B1%E8%AF%AD |
如果仅对一个普通字符串,使用urlencode,则提示以下内容:
|-----------|----------------------------------------------------------------------|
| 1 | TypeError: not a valid non-string sequence or mapping object |
翻译过来:
类型错误:不是有效的非字符串序列或映射
也就是说urlencode只能转字典这类型的。如果一定要去转字符串,则应该使用:quote,使用示例如下:
|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | from urllib.parse import quote print(quote("中国123")) #输出:%E4%B8%AD%E5%9B%BD123 print(quote("中国123", encoding="GBK")) #输出:%D6%D0%B9%FA123 |
参考链接:https://blog.csdn.net/yzx99/article/details/126568279
51工具盒子