I am using the code below based on this answer to store authentication cookies in a file. This allows me to avoid having to log in every time I run the program.
Now, fetching the csrftoken from regular Session cookies works well, but when I try to fetch it from the cookiejar using csrf = s.cookies['csrftoken']
, I get
AttributeError: LWPCookieJar instance has no attribute '__getitem__'`
I understand that this happens because I treat s.cookies as a list even though is now a LWPCookieJar object. I suppose this leaves me with two options:
Either:
- Extract the list from the
LWPCookieJar
object and then getting the csrf token is easy. - or, Find the appropriate syntax to retrieve the csrf token directly from the
LWPCookieJar
.
How can I do this?
import os
import requests
from cookielib import LWPCookieJar
s = requests.Session()
s.cookies = LWPCookieJar('cookiejar')
if os.path.exists('cookiejar'):
# Load saved cookies from the file and use them in a request
print('loading saved cookies')
s.cookies.load()
else:
# Create a new cookies file and set our Session's cookies
print('saving cookies')
s.cookies.save()
r = s.get('http://httpbin.org/cookies')
print(r.text)
# Save the session's cookies back to the file
s.cookies.save()
I finally figured it out. Not sure if it can be further simplified.