按名称从CookieJar获取Cookie [英] Get cookie from CookieJar by name
问题描述
我知道我可以在cookiejar中迭代cookie,这将允许我找到一个特定名称的cookie - 但CookieJar对象本身有什么方法可以调用来获取一个特定的cookie名称?
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.
推荐答案
是的, __ iter __
方法将通过 CookieJar
中的每个cookie。
Yes, the __iter__
method will go through each cookie in CookieJar
.
for cookie in cj:
print cookie.name, cookie.value, cookie.domain #etc etc
Cookie不仅仅是名称和值对。在其长列表(17)的属性中,有 domain
和 path
。域值 .ibm.com
将适用于网站 mail.ibm.com
。域值 ibm.com
和路径值 / abc
不适用于网页 ibm.com/index.htm
。因此,通过提供单独的名称不足以在 CookieJar
中找到适用的cookie的值。
A cookie is not just a name and value pair. In its long list (17) of properties, there is domain
and path
. A domain value of .ibm.com
would be applicable to the website mail.ibm.com
for example. A domain value of ibm.com
and path value of /abc
would not apply to the web page ibm.com/index.htm
. So by supplying the name alone is insufficient to find the value of an applicable cookie in CookieJar
.
code> __ iter __ 方法将返回一个 cookie
对象的列表,例如 list(cj)
, CookieJar
的内部结构不是一个简单的列表。 CookieJar
类的内部是这里。
Though the __iter__
method will return a list of cookie
objects easily, example list(cj)
, the internal structure of CookieJar
is not a simple list. Internals about the CookieJar
class is here.
这篇关于按名称从CookieJar获取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!