python url参数转dict

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': '1', '_sop': '13', 'LH_Complete': '1', 'LH_Sold': '1', '_udlo': '24', '_udhi': '48', '_ipg': '200', '_pgn': '1', '_skc': '0'}



反过来:
res={'_from': 'R40', 'LH_BIN': '1', '_sop': '13', 'LH_Complete': '1', 'LH_Sold': '1', '_udlo': '24', '_udhi': '48', '_ipg': '200', '_pgn': '1', '_skc': '0'}
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

 

posted @ 2018-08-29 16:46  wangheng1409  阅读(4709)  评论(0编辑  收藏  举报