I am trying to parse the cookies from a website set by the Set-Cookie
header for their expiry period. I am using the requests library and this is how far I've progressed:
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))
However, cook.expires
returns a value incomprehendible, something like 2181183994
and 1550035594
. What needs to be done next to get the exact date time value from the expires flag?
The cook.expires
value you're getting is a timestamp value. You just need to use fromtimestamp()
from the datetime
library to resolve this. Here is your code:
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))
For example, if your url is https://github.com
, you get it in the human readable format:
2039-02-13 10:30:14 # expiry date for first cookie
2019-02-13 11:30:14 # expiry date for second cookie