我试图获取从服务器检索的特定cookie的到期时间:
s = requests.session()
r = s.get("http://localhost/test")
r.cookies
这将列出服务器发送的所有cookie(我得到2个cookie):
<<class 'requests.cookies.RequestsCookieJar'>[<Cookie PHPSESSID=cusa6hbtb85li8po
argcgev221 for localhost.local/>, <Cookie WebSecu=f for localhost.local/test>]>
当我这样做时:
r.cookies.keys
我明白了:
<bound method RequestsCookieJar.items of <<class 'requests.cookies.RequestsCooki
eJar'>[Cookie(version=0, name='PHPSESSID', value='30tg9vn9376kmh60ana2essfi3', p
ort=None, port_specified=False, domain='localhost.local', domain_specified=False
, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires
=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False), Co
okie(version=0, name='WebSecu', value='f', port=None, port_specified=False, doma
in='localhost.local', domain_specified=False, domain_initial_dot=False, path='/test', path_specified=False, secure=False, expires=1395491371, discard=Fals
e, comment=None, comment_url=None, rest={}, rfc2109=False)]>>
如您所见,我们有两个Cookie。我想获得名为&#34; WebSecu&#34;
的cookie的到期时间谢谢
答案 0 :(得分:10)
在requests
中,cookie jar是一个非常特殊的对象。如果你这样做,你可能会注意到:
r.cookies['WebSecu']
您将以字符串形式接收该Cookie的值(在您的示例f
中)。要获取包含该信息的实际cookie对象,您必须像这样迭代cookie jar:
expires = None
for cookie in r.cookies:
if cookie.name == 'WebSecu':
expires = cookie.expires