How to urlencode a querystring in Python?

后端 未结 13 1720
猫巷女王i
猫巷女王i 2020-11-22 11:19

I am trying to urlencode this string before I submit.

queryString = \'eventName=\' + evt.fields[\"eventName\"] + \'&\' + \'eventDescription=\' + evt.fie         


        
13条回答
  •  名媛妹妹
    2020-11-22 11:36

    You need to pass your parameters into urlencode() as either a mapping (dict), or a sequence of 2-tuples, like:

    >>> import urllib
    >>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'}
    >>> urllib.urlencode(f)
    'eventName=myEvent&eventDescription=cool+event'
    

    Python 3 or above

    Use:

    >>> urllib.parse.urlencode(f)
    eventName=myEvent&eventDescription=cool+event
    

    Note that this does not do url encoding in the commonly used sense (look at the output). For that use urllib.parse.quote_plus.

提交回复
热议问题