我可以执行以下操作来构建 url:
base_url = 'http://google.com/'
qs = urllib.urlencode({'q': string})
url = base_url + '?' + qs
有没有办法对字符串进行 url 编码?例如,我希望能够做到:
url = 'http://google.com/?q=' + urlencode('this is my search'))
最佳答案
使用urllib.quote
或urllib.quote_plus
,例如:
>>> urllib.quote('this is my string')
'this%20is%20my%20string'
>>> urllib.quote_plus('this is my string')
'this+is+my+string'
关于python - 对字符串进行 URL 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28890909/