python将原生cookie转换为字典dict格式

使用python将获取的原生cookie转换为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)

结果:
{
"thw": "cn;nk=%5Cu65E0%5Cu58F0%5Cu6EF4%5Cu5BF9%5Cu767D",
"cookie17": "UonZBGCaYSPQhQ%3D%3D",
"l": "AiIimIoKS0HVED4ao4GprqAT8qKEcyaN",
"l_g": "Ug%3D%3D",
"cna": "xaYUEGbE2X0CAd7f2Qhq4DAA",
"isg": "AkxMGzVSdzRypWPIho7m3FdsHapqgvAvBpI7WaYNWPeaMew7zpXAv0KDp47z"
}

你可能感兴趣的:(python将原生cookie转换为字典dict格式)