如何在Python中對查詢字串進行urlencode?
P粉245489391
P粉245489391 2023-08-23 14:36:44
0
2
390
<p>我正在嘗試在提交之前對該字串進行 urlencode。 </p> <pre class="brush:php;toolbar:false;">queryString = 'eventName=' evt.fields["eventName"] '&' 'eventDescription=' evt.fields["eventDescription"];< /pre> <p><br /></p>
P粉245489391
P粉245489391

全部回覆(2)
P粉270891688

Python 2

您要找的是 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

在 Python 3 中,urllib 套件已被分解為更小的元件。您將使用 urllib.parse.quote_plus (注意 parse 子模組)

import urllib.parse
safe_string = urllib.parse.quote_plus(...)
P粉562845941

您需要將參數傳遞到 urlencode() 作為映射(字典)或二元組序列,例如:

>>> 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代码>.

#
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!