Use instead of + for space in python query parameters

前端 未结 7 1278
猫巷女王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-04 00:11

    I only find urllib.parse.quote , which can replace space to %20 .

    But quote could not convert a dict.

    so, We must use quote to transform dict in advance.


    #for python3
    from urllib.parse import quote
    
    payload = {'key1': 'value  1', 'key2': 'value 2'}
    
    newpayload = {}
    for (k, v) in payload.items():
        newpayload[quote(k)] = quote(v)
    print(newpayload)
    #print result: {'key1': 'value%20%201', 'key2': 'value%202'}
    # Now, you can use it in requests
    

提交回复
热议问题