如何在Python中使用Urlencode查询字符串?
我尝试在提交之前对该字符串进行urlencode
。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
-
你需要将参数传递
urlencode()
为映射(dict)
或2元组序列,例如:>>> import urllib >>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'} >>> urllib.urlencode(f) 'eventName=myEvent&eventDescription=cool+event'
Python 3或以上
采用:
>>> urllib.parse.urlencode(f) eventName=myEvent&eventDescription=cool+event
请注意,这在通常意义上不会进行url编码(请看输出)。为此使用
urllib.parse.quote_plus
。 -
Python 2
你正在寻找的是
urllib.quote_plus
:>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$') 'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
Python 3
在Python 3中,该urllib软件包已分解为较小的组件。你将使用
urllib.parse.quote_plus
(注意parse子模块)import urllib.parse urllib.parse.quote_plus(...)