How do you encode a label containing a slash as one URL path segment?
quote percent-encodes a component. An empty safe set also encodes slash so it can represent data inside one path segment.
Python example and interpretation
Encode the complete label 猫/狗 before adding it to a fixed route. Encoding a whole URL as one component would also transform its structural delimiters.
from urllib.parse import quote
quote('猫/狗',safe='')Result '%E7%8C%AB%2F%E7%8B%97'
中文
标注名称带有斜杠时,怎样把它放入单个 URL 路径段?
quote 将字符按 URL 百分号编码。传入空 safe 可让斜杠也编码,不再承担段分隔符的含义。
Python 算例与适用条件
标签“猫/狗”应先作为完整名称编码,再与固定路径拼接。若整条 URL 一起编码,会同时改写协议和分隔符。
from urllib.parse import quote
quote('猫/狗',safe='')结果 '%E7%8C%AB%2F%E7%8B%97'