URL Encoder / Decoder
对 URL 和查询参数进行编码和解码。
URL 编码器和解码器帮助您将特殊字符安全地转换为百分比编码格式以用于 URL,以及将百分比编码的字符串解码回人类可读的文本。空格、&、= 和 # 等特殊字符在 URL 中具有特定含义,必须编码才能正确传输。
将 URL 或查询参数值粘贴到编码器中,工具会将每个不安全字符替换为 % 加上两位十六进制代码。解码器执行相反的过程,将 %20 还原为空格,将 %26 还原为 &。
常见用例包括为 API 请求构建查询字符串、调试 Webhook 有效负载以及修复包含未编码特殊字符的损坏链接。所有处理均在您的浏览器中进行,不会将数据发送到外部。
常见问题
代码实现
from urllib.parse import quote, unquote, quote_plus, urlencode, urlparse
# Encode a single value (for use in path or query value)
raw = "hello world & more/stuff"
encoded = quote(raw)
print(encoded) # hello%20world%20%26%20more%2Fstuff
# query-string style: spaces become +, / is also encoded
qs_encoded = quote_plus(raw)
print(qs_encoded) # hello+world+%26+more%2Fstuff
# Decode
decoded = unquote(encoded)
print(decoded) # hello world & more/stuff
# Encode a full query string from a dict
params = {"name": "Alice Smith", "city": "São Paulo", "q": "a+b=c&d"}
query = urlencode(params)
print(query) # name=Alice+Smith&city=S%C3%A3o+Paulo&q=a%2Bb%3Dc%26d
# Parse a URL and re-encode its components
url = "https://example.com/search?q=hello world&lang=en"
parsed = urlparse(url)
print(parsed.query) # q=hello world&lang=en (raw)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.