搜尋

首頁  >  問答  >  主體

如何在Python中對查詢字串進行urlencode?

<p>我正在嘗試在提交之前對該字串進行 urlencode。 </p> <pre class="brush:php;toolbar:false;">queryString = 'eventName=' evt.fields["eventName"] '&' 'eventDescription=' evt.fields["eventDescription"];< /pre> <p><br /></p>
P粉245489391P粉245489391543 天前522

全部回覆(2)我來回復

  • P粉270891688

    P粉2708916882023-08-24 11:41:36

    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< /code> (注意 parse 子模組)

    import urllib.parse
    safe_string = urllib.parse.quote_plus(...)

    回覆
    0
  • P粉562845941

    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.

    #

    回覆
    0
  • 取消回覆