urlencode编码,解码

时间:2024-11-23 16:33:20

对字符串传入的字典参数进行urlencode编码,就需要用到两个方法urlencode和quote
urlencode方法传字典参数

from urllib.parse import urlencode, quote, unquote

# urlencode方法参数是字典

body = {
"content": "呃呃呃",
"charsetSelect": "utf-8",
"en": "UrlEncode编码"
}
print(urlencode(body))

quote传字符串参数

# quote方法参数是字符串

print(quote("上海-悠悠"))

url = "http://www.example.com/?a=问问为&b=同人文"
print(quote(url))
======================================================
import requests
from urllib.parse import urlencode, quote, unquote url = "http://www.example.com/"
par = {
"a": "问问",
"b": "嗯嗯"
}
body = {
"content": "嗯嗯",
"charsetSelect": "utf-8",
"en": "UrlEncode编码"
} r = requests.post(url, params=par, data=body)
print(r.url)
print(unquote(r.url))