from urllib.parse import urlencode, quotefrom oauthlib.common import urldecodedef decodeUrl(url): """ :param url: 傳入一個待解碼的 連接 :return: 輸出 元組 url , 包含參數的字典 """ firstUrl, paramStr = url.split('?', 1) return firstUrl, {item[0]: item[1] for item in urldecode(paramStr)}def encodeUrl(mainUrl, paramsData): """ :param mainUrl: 待合併url :param paramsData: 參數字典 :return: 合併後的url """ return quote('%s?%s' % (mainUrl, urlencode(paramsData)))print(encodeUrl('https://www.baidu.com/', {'a': 3, 'b': 'sdafj'}))print(decodeUrl( 'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1571379106429_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&sid=&word=%E8%87%AA%E7%84%B6'))