在 Python 中,斜杠(/)是一个特殊字符,它在 URL 编码中需要被转义。
可以使用 urllib.parse 模块中的 quote() 函数进行 URL 编码,如下所示:
import urllib.parse
url = "https://www.example.com/path/to/file/"
encoded_url = urllib.parse.quote(url)
print(encoded_url)
该代码将生成以下输出:
https%3A//www.example.com/path/to/file/
quote() 函数会对 URL 中的每个字符进行编码,其中斜杠也不例外。