python url参数转dict

时间:2024-01-21 15:47:57
from urllib import parse
url='_from=R40&LH_BIN=1&_sop=13&LH_Complete=1&LH_Sold=1&_udlo=24&_udhi=48&_ipg=200&_pgn=1&_skc=0'
res = dict(parse.parse_qsl(url)) print(res) 或:
res={k.split('=')[0]: k.split('=')[1] for k in url.split('&')} 或:
res=dict(k.split('=') for k in url.split('&')) >>>
{'_from': 'R40', 'LH_BIN': '', '_sop': '', 'LH_Complete': '', 'LH_Sold': '', '_udlo': '', '_udhi': '', '_ipg': '', '_pgn': '', '_skc': ''} 反过来:
res={'_from': 'R40', 'LH_BIN': '', '_sop': '', 'LH_Complete': '', 'LH_Sold': '', '_udlo': '', '_udhi': '', '_ipg': '', '_pgn': '', '_skc': ''}
parameters = parse.urlencode(res)
print(parameters)
>>>
_from=R40&LH_BIN=1&_sop=13&LH_Complete=1&LH_Sold=1&_udlo=24&_udhi=48&_ipg=200&_pgn=1&_skc=0