Python中对URL进行转义需要使用urllib库中的quote()函数,示例代码如下:

import urllib.parse

url = "https://www.example.com/path?param1=value1&param2=value2#fragment"
escaped_url = urllib.parse.quote(url, safe=':/?=&#')
print(escaped_url)

输出结果为:

https%3A//www.example.com/path%3Fparam1%3Dvalue1%26param2%3Dvalue2%23fragment

其中,safe参数指定哪些字符不需要转义。在上面的示例中,我们将冒号、斜杠、问号、等号和井号设置为不需要转义的字符。

python转义网址

原文地址: https://www.cveoy.top/t/topic/EeQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录