使用python將獲取的原生cookie轉(zhuǎn)換為dict
代碼如下:
import json
cookie = 'cna=xaYUEGbE2X0CAd7f2Qhq4DAA; thw=cn;nk=%5Cu65E0%5Cu58F0%5Cu6EF4%5Cu5BF9%5Cu767D; l_g=Ug%3D%3D; cookie17=UonZBGCaYSPQhQ%3D%3D; l=AiIimIoKS0HVED4ao4GprqAT8qKEcyaN; isg=AkxMGzVSdzRypWPIho7m3FdsHapqgvAvBpI7WaYNWPeaMew7zpXAv0KDp47z'
cookieDict = {}
cookies = cookie.split("; ")
for co in cookies:
co = co.strip()
p = co.split('=')
value = co.replace(p[0]+'=', '').replace('"', '')
cookieDict.setitem(p[0], value)
print json.dumps(cookieDict, indent=2)
結(jié)果:
{
"thw": "cn;nk=%5Cu65E0%5Cu58F0%5Cu6EF4%5Cu5BF9%5Cu767D",
"cookie17": "UonZBGCaYSPQhQ%3D%3D",
"l": "AiIimIoKS0HVED4ao4GprqAT8qKEcyaN",
"l_g": "Ug%3D%3D",
"cna": "xaYUEGbE2X0CAd7f2Qhq4DAA",
"isg": "AkxMGzVSdzRypWPIho7m3FdsHapqgvAvBpI7WaYNWPeaMew7zpXAv0KDp47z"
}