P粉2708916882023-08-24 11:41:36
您要找的是 urllib.quote_plus
:
safe_string = urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$') #Value: 'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
在 Python 3 中,urllib
套件已被分解為更小的元件。您將使用 urllib.parse.quote_plus< /code>
(注意 parse
子模組)
import urllib.parse safe_string = urllib.parse.quote_plus(...)
P粉5628459412023-08-24 00:39:26
您需要將參數傳遞到 urlencode()< /code>
作為映射(字典)或二元組序列,例如:
>>> import urllib >>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'} >>> urllib.urlencode(f) 'eventName=myEvent&eventDescription=cool+event'
Python 3 或更高版本
使用urllib.parse.urlencode< /代码>
:
>>> urllib.parse.urlencode(f) eventName=myEvent&eventDescription=cool+event
請注意,這不執行常用意義上的 url 編碼(查看輸出)。為此,請使用 urllib.parse.quote_plus代码>
.