Use instead of + for space in python query parameters

前端 未结 7 1281
猫巷女王i
猫巷女王i 2021-02-03 23:30

I have written the following python script, using python requests (http://requests.readthedocs.org/en/latest/):

import requests

payload = {\'key1\': \'value  1\         


        
7条回答
  •  情书的邮戳
    2021-02-03 23:49

    We can use urllib2.Request to call url

    import urllib2
    
    send_params = {'key1': 'value  1', 'key2': 'value 2'}
    new_send_params = []
    for (k, v) in send_params.items():
        new_send_params.append(k + "=" + urllib2.quote(v))
    
    url = 'http://example.com/service?'+ '&'.join(new_send_params)
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    print "Request URL: " + url
    #Request URL: http://example.com/service?key1=value%20&key2=value%202
    print response.read()
    #Python Master Request handler 2016-07-04 16:05:19.928132 . Your request path is  /service?key1=value%20&key2=value%202
    

提交回复
热议问题