Get cookie from CookieJar by name

后端 未结 4 1886
半阙折子戏
半阙折子戏 2021-01-03 19:46

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 20:31

    Yes, the __iter__ method will go through each cookie in CookieJar.

    for cookie in cj:
       print cookie.name, cookie.value, cookie.domain #etc etc
    

    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.

    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.

提交回复
热议问题