我试图解析由Set-Cookie
标题设置的网站的cookie到期。我正在使用请求库,这是我进步的程度:
import requests
req = requests.get(url, headers=headers)
if req.cookies:
for cook in req.cookies:
if cook.expires: # checking the expires flag
print('Cookie Expiry Time: %s' % (cook.expires))
然而,cook.expires
返回一个难以理解的值,像2181183994
和1550035594
。接下来需要做什么才能从expires标志中获取确切的日期时间值?
您获得的cook.expires
值是时间戳值。你只需要使用fromtimestamp()
库中的datetime
来解决这个问题。这是你的代码:
import requests
from datetime import datetime
req = requests.get(url, headers=headers)
if req.cookies:
for cook in req.cookies:
if cook.expires: # checking the expires flag
print(datetime.fromtimestamp(cook.expires))
例如,如果您的网址是https://github.com
,则可以使用人类可读的格式:
2039-02-13 10:30:14 # expiry date for first cookie
2019-02-13 11:30:14 # expiry date for second cookie