我试图在提交之前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中,这对我来说很有效

import urllib

urllib.parse.quote(query)

供以后参考(例如:For python3)

>>> import urllib.request as req
>>> query = 'eventName=theEvent&eventDescription=testDesc'
>>> req.pathname2url(query)
>>> 'eventName%3DtheEvent%26eventDescription%3DtestDesc'

另一件可能还没有提到的事情是urllib.urlencode()将字典中的空值编码为字符串None,而不是将该形参作为不存在的参数。我不知道这是否是通常需要的,但不适合我的用例,因此我必须使用quote_plus。

Python 3:

urllib parse。引用+(字符串,安全= ",编码= no, errors= no)

尝试请求而不是urllib,你不需要打扰urlencode!

import requests
requests.get('http://youraddress.com', params=evt.fields)

编辑:

如果你需要有序的名称-值对或多个名称值,那么设置参数如下:

params=[('name1','value11'), ('name1','value12'), ('name2','value21'), ...]

而不是用字典。