假设我有以下字符串:
"http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large"
Run Code Online (Sandbox Code Playgroud)
是否有一些函数或模块能够将上面的字符串转换为下面的字符串,其中所有字符都更改为符合url:
"http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz%3Fa%3Dlarge"
Run Code Online (Sandbox Code Playgroud)
在python中执行此操作的最佳方法是什么?
Col*_*lau 18
Python 2的urllib.quote_plus和Python 3的urllib.parse.quote_plus
url = "http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large"
# Python 2
urllib.quote_plus(url)
# Python 3
urllib.parse.quote_plus(url)
Run Code Online (Sandbox Code Playgroud)
输出:
'http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz%3Fa%3Dlarge'
Run Code Online (Sandbox Code Playgroud)
小智 6
在 Windows 平台上可用
#! python3.6
from urllib.parse import quote
# result: http://www.oschina.net/search?scope=bbs&q=C%E8%AF%AD%E8%A8%80
quote('http://www.oschina.net/search?scope=bbs&q=C??',safe='/:?=&')
Run Code Online (Sandbox Code Playgroud)