I know that I can iterate through the cookies in a cookiejar, and this would allow me to find a cookie with a particular name - but does the CookieJar object itself have any methods I can call to get a certain cookie by name?
It just saves me having to write a helper method that already exists.
cookielib.CookieJar?
you can convert jar to a list and process that, e.g.
{i.name: i for i in list(j)}
and btw, j._cookies is actually a dict-dict already, though not completely trivially indexed.
cookie jar file?
I thought those were plain text files...
Yes, the
__iter__
method will go through each cookie inCookieJar
.A cookie is not just a name and value pair. In its long list (17) of properties, there is
domain
andpath
. A domain value of.ibm.com
would be applicable to the websitemail.ibm.com
for example. A domain value ofibm.com
and path value of/abc
would not apply to the web pageibm.com/index.htm
. So by supplying the name alone is insufficient to find the value of an applicable cookie inCookieJar
.Though the
__iter__
method will return a list ofcookie
objects easily, examplelist(cj)
, the internal structure ofCookieJar
is not a simple list. Internals about theCookieJar
class is here.It's undocumented internals, but you can access cookies directly like this:
cookiejar._cookies[domain][path][name]
You can also use dict_from_cookiejar, which returns a key/value dictionary from a CookieJar. Something like:
and then access your cookie value by key.