如何在Python中使用Urlencode查询字符串? [英] How to urlencode a querystring in Python?
本文介绍了如何在Python中使用Urlencode查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我提交之前,我正在尝试对该字符串进行urlencode.
I am trying to urlencode this string before I submit.
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
推荐答案
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或更高版本
使用:
>>> urllib.parse.urlencode(f)
eventName=myEvent&eventDescription=cool+event
请注意,通常情况下,不会执行url编码(请看输出).为此,请使用urllib.parse.quote_plus
.
Note that this does not do url encoding in the commonly used sense (look at the output). For that use urllib.parse.quote_plus
.
这篇关于如何在Python中使用Urlencode查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文