您好,登錄后才能下訂單哦!
平時見到的url參數都是key-value, 一般vlaue都是字符串類型的
如果有幸和我一樣遇到字典,列表等參數,那么就幸運了
python2代碼
import json from urllib import urlencode # 1. 直接將url編碼 params = { "name": "Tom", "hobby": ["ball", "swimming"], "books": [{"name": "語文", "pages": 20}] } print(urlencode(params)) """ hobby=%5B%27ball%27%2C+%27swimming%27%5D& books=%5B%7B%27name%27%3A+%27%5Cxe8%5Cxaf%5Cxad%5Cxe6%5Cx96%5Cx87%27%2C+%27pages%27%3A+20%7D%5D& name=Tom """ # 2. 先用json序列化字典和列表參數 params = { "name": "Tom", "hobby": json.dumps(["ball", "swimming"]), "books": json.dumps([{"name": "語文", "pages": 20}]) } print(urlencode(params)) """ hobby=%5B%22ball%22%2C+%22swimming%22%5D& books=%5B%7B%22name%22%3A+%22%5Cu8bed%5Cu6587%22%2C+%22pages%22%3A+20%7D%5D& name=Tom """
觀察1 和2 序列化出來的url參數不是一樣的,如果第一種方式的url不能正常訪問,那么就用第二種
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。