在提交之前,我试图对这个字符串进行urlencode。
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
你需要的是 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中,urllib
包已经被拆分成较小的组件。您将使用urllib.parse.quote_plus
(请注意parse
子模块)。
import urllib.parse
safe_string = urllib.parse.quote_plus(...)
您需要将参数作为映射(字典)或2元组序列传递到urlencode()
中:
>>> 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
urllib.parse.quote_plus
。尝试使用 requests 代替 urllib,您就不需要烦恼 urlencode 了!
import requests
requests.get('http://youraddress.com', params=evt.fields)
编辑:
如果您需要有序的名称-值对或一个名称的多个值,请设置参数如下:
params=[('name1','value11'), ('name1','value12'), ('name2','value21'), ...]
使用字典的代替方法。
以下是一个完整的解决方案,包括如何处理一些注意事项。
### ********************
## init python (version 2.7.2 )
import urllib
### ********************
## first setup a dictionary of name-value pairs
dict_name_value_pairs = {
"bravo" : "True != False",
"alpha" : "http://www.example.com",
"charlie" : "hello world",
"delta" : "1234567 !@#$%^&*",
"echo" : "user@example.com",
}
### ********************
## setup an exact ordering for the name-value pairs
ary_ordered_names = []
ary_ordered_names.append('alpha')
ary_ordered_names.append('bravo')
ary_ordered_names.append('charlie')
ary_ordered_names.append('delta')
ary_ordered_names.append('echo')
### ********************
## show the output results
if('NO we DO NOT care about the ordering of name-value pairs'):
queryString = urllib.urlencode(dict_name_value_pairs)
print queryString
"""
echo=user%40example.com&bravo=True+%21%3D+False&delta=1234567+%21%40%23%24%25%5E%26%2A&charlie=hello+world&alpha=http%3A%2F%2Fwww.example.com
"""
if('YES we DO care about the ordering of name-value pairs'):
queryString = "&".join( [ item+'='+urllib.quote_plus(dict_name_value_pairs[item]) for item in ary_ordered_names ] )
print queryString
"""
alpha=http%3A%2F%2Fwww.example.com&bravo=True+%21%3D+False&charlie=hello+world&delta=1234567+%21%40%23%24%25%5E%26%2A&echo=user%40example.com
"""
Python 3:
urllib.parse.quote_plus(string, safe='', encoding=None, errors=None)
Python 3:
{{link1:urllib.parse.quote_plus(string,safe ='',encoding = None,errors = None)
}}
urllib.parse.quote()
пјҢеӣ дёәе®ғдҪҝз”Ё%20
иҖҢдёҚжҳҜ+
жқҘд»ЈиЎЁз©әж јгҖӮ - binki试试这个:
urllib.pathname2url(stringToURLEncode)
urlencode
无法使用,因为它只适用于字典。 quote_plus
未能产生正确的输出。
my string
转换为 my%20string
。你的解决方案对此非常有效! - TanguyP%20
代替+
有效。谢谢。 - Jossef Harush Kadouriurllib.request.pathname2url
。 - Alex Montoya请注意,urllib.urlencode 并不总是能达到预期效果。问题在于,有些服务关心参数的顺序,在创建字典时可能会丢失。对于这种情况,正如Ricky所建议的那样,使用 urllib.quote_plus 更好。
>>> import urllib
>>> urllib.urlencode([('name', 'brandon'), ('uid', 1000)])
'name=brandon&uid=1000'
- Brandon Rhodesimport urllib
urllib.parse.quote(query)
import urllib.parse
,否则会出现"AttributeError: module 'urllib' has no attribute 'parse'"的错误。 - undefined供日后参考(例如:针对Python3)
>>> import urllib.request as req
>>> query = 'eventName=theEvent&eventDescription=testDesc'
>>> req.pathname2url(query)
>>> 'eventName%3DtheEvent%26eventDescription%3DtestDesc'
'c:/2 < 3'
在 Windows 上的输出是 '///C://2%20%3C%203'
。我想要的是只输出 'c:/2%20%3C%203'
。 - binkiimport urllib.parse
query = 'Hellö Wörld@Python'
urllib.parse.quote(query) // returns Hell%C3%B6%20W%C3%B6rld%40Python
import urllib
,而应该是urllib.parse
。是的,他们也可以建议进行编辑,但就目前而言,这个答案是两个中最正确的一个。 - undefined
import urllib.parse ... urllib.parse.quote_plus(query)
- ivkremerpython3 -c "import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1])) "
,用于编码字符串。 - Amos Joshua"
,应该是:python3 -c "import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1]))" "要编码的字符串"
- lgespee