我试图在提交之前urlencode这个字符串。

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

当前回答

Python 3:

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

其他回答

如果urllib.parse. parse。urlencode()给你错误,然后尝试urllib3模块。

语法如下:

import urllib3
urllib3.request.urlencode({"user" : "john" }) 

试试这个:

urllib.pathname2url(stringToURLEncode)

Urlencode不能工作,因为它只对字典有效。Quote_plus没有产生正确的输出。

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

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

注意urllib。Urlencode并不总是能做到这一点。问题在于,有些服务关心参数的顺序,而在创建字典时,这些顺序就会丢失。对于这种情况,使用urllib。正如Ricky建议的那样,quote_plus更好。

Python 3:

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