[在响应头中返回多个时提取某个“ set-cookie”值

问题描述 投票:0回答:1

我正在使用请求,我需要从响应标头集cookie中提取特定值。我不能使用r.cookies,因为它不会添加过期,路径,域等,因此我需要这些值。

当我这样做

test = r.headers['set-cookie']
print(test)

我得到了这样的答复:

'cookie1 = cookie1value; expires=datehere; path=/; domain=domainhere, cookie2 = cookie2value; expires=datehere; path=/; domain=domainhere,cookie3 = cookie3value; Domain=.domain.com; Path=/; Expires=Wed, 04 Nov 2020 19:44:17 GMT; Max-Age=31536000; Secure

我需要使用其所有标签提取cookie3的值。

python python-requests response-headers
1个回答
0
投票

您可以使用re

import re

test = 'cookie1 = cookie1value; expires=datehere; path=/; domain=domainhere, cookie2 = cookie2value; expires=datehere; path=/; domain=domainhere,cookie3 = cookie3value; expires=datehere; path=/; domain=domainhere,cookie4 = cookie4value; expires=datehere; path=/; domain=domainhere'

p = re.compile(r'cookie3 = (.*?),cookie4')
print(p.findall(test)[0])
© www.soinside.com 2019 - 2024. All rights reserved.