python cookies过期_python - 获取cookie到期时间使用请求库

I am trying to get the expiry time of a specific cookie that I retrieve from the server as:

s = requests.session()

r = s.get("http://localhost/test")

r.cookies

This will list all cookies sent by the server (I get 2 cookies) as:

<[

argcgev221 for localhost.local/>, ]>

When I do:

r.cookies.keys

I get:

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)]>>

As you can see, we have two cookies. I would like to get the expiry time of the cookie named "WebSecu"

Thank you

解决方案

In requests, the cookie jar is a very special object. You might notice that if you do:

r.cookies['WebSecu']

You'll receive the value of that cookie as a string (in your example f). To get the actual cookie object that holds that information, you will have to iterate over the cookie jar like so:

expires = None

for cookie in r.cookies:

if cookie.name == 'WebSecu':

expires = cookie.expires

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值