如何从response['set-cookie']响应中转换httplib2输出字符串,例如

"cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"


{'cookie1':'xxxyyyzzz','cookies2':'abcdef'}

最佳答案

使用 http.cookies :

>>> c = "cookie1=xxxyyyzzz;Path=/;Expires=Wed, 03-Feb-2015 08:03:12 GMT;Secure;HttpOnly, cookie2=abcdef;Path=/;Secure"
>>> from http.cookies import SimpleCookie
>>> cookie = SimpleCookie()
>>> cookie.load(c)
>>> cookie
<SimpleCookie: cookie1='xxxyyyzzz' cookie2='abcdef'>
>>> {key: value.value  for key, value in cookie.items()}
{'cookie1': 'xxxyyyzzz', 'cookie2': 'abcdef'}

关于python - Python-将set-cookies响应转换为cookie的dict,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21522586/

10-12 19:33