我试图在提交之前urlencode这个字符串。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
我试图在提交之前urlencode这个字符串。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
当前回答
为了在需要同时支持python 2和3的脚本/程序中使用,six模块提供了quote和urlencode函数:
>>> from six.moves.urllib.parse import urlencode, quote
>>> data = {'some': 'query', 'for': 'encoding'}
>>> urlencode(data)
'some=query&for=encoding'
>>> url = '/some/url/with spaces and %;!<>&'
>>> quote(url)
'/some/url/with%20spaces%20and%20%25%3B%21%3C%3E%26'
其他回答
Python 3:
urllib parse。引用+(字符串,安全= ",编码= no, errors= no)
供以后参考(例如:For python3)
>>> import urllib.request as req
>>> query = 'eventName=theEvent&eventDescription=testDesc'
>>> req.pathname2url(query)
>>> 'eventName%3DtheEvent%26eventDescription%3DtestDesc'
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(注意解析子模块)
import urllib.parse
safe_string = urllib.parse.quote_plus(...)
你需要将你的参数传递给urlencode()作为一个映射(dict),或者一个2元组序列,比如:
>>> 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。
试试这个:
urllib.pathname2url(stringToURLEncode)
Urlencode不能工作,因为它只对字典有效。Quote_plus没有产生正确的输出。