最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中使用cookielib的FileCookieJar去save(),结果出错:NotImplementedError

Python crifan 5318浏览 0评论

【问题】

用python代码:

import cookielib;
gVal['cj'] = cookielib.FileCookieJar(localCookieFileName);

#create cookie file
gVal['cj'].save();

结果出错:

    crifanLib.initAutoHandleCookies("localCookieFile.txt");

  File "libs\crifanLib.py", line 705, in initAutoHandleCookies

    gVal[‘cj’].save();

  File "D:\tmp\dev_install_root\Python27_x64\lib\cookielib.py", line 1753, in save

    raise NotImplementedError()

NotImplementedError

 

【解决过程】

1.之前已经大概看了,python的手册的解释了:

FileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)

Save cookies to a file.

This base class raises NotImplementedError. Subclasses may leave this method unimplemented.

filename is the name of file in which to save cookies. If filename is not specified, self.filename is used (whose default is the value passed to the constructor, if any); if self.filename is None, ValueError is raised.

ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expired

The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later using the load() or revert() methods.

但是不太懂。

2.参考:

Python FileCookieJar.save() issue

得知,可以改用MozillaCookieJar或LWPCookieJar。

3.所以去试了试:

针对

http://www.google.com/

 

  • 使用LWPCookieJar的代码:
import cookielib;
gVal['cj'] = cookielib.LWPCookieJar(localCookieFileName);

#create cookie file
gVal['cj'].save();

 

    • 得到cookie结果是:

      #LWP-Cookies-2.0

      Set-Cookie3: PREF="ID=4d37691401c67131:FF=0:NW=1:TM=1358232482:LM=1358232482:S=uJuXpzeOCHiPXC6n"; path="/"; domain=".google.com"; path_spec; domain_dot; expires="2015-01-15 06:48:02Z"; version=0

      Set-Cookie3: NID="67=kHDkitb44zdLIFhq_X20E17jh0wOpAId1yiPja9butDIsD1Cefs2YaqSZPYuVEysTpcRCoOSKBtCLzMDqHJRXHgtlymYIBW7XvztYXrtDEdTfhj_Q4vsWOT9bms69bGv"; path="/"; domain=".google.com.hk"; path_spec; domain_dot; expires="2013-07-17 06:48:02Z"; HttpOnly=None; version=0

      Set-Cookie3: PREF="ID=c6950eb3132a355d:U=64ec79359ad60919:FF=2:LD=zh-CN:NW=1:TM=1358232482:LM=1358232482:S=WvKpG_uygzjmn_Lr"; path="/"; domain=".google.com.hk"; path_spec; domain_dot; expires="2015-01-15 06:48:02Z"; version=0

    • 对应官网解释是:
        class cookielib.LWPCookieJar(filename, delayload=None, policy=None)

        A FileCookieJar that can load from and save cookies to disk in format compatible with the libwww-perl library’s Set-Cookie3 file format. This is convenient if you want to store cookies in a human-readable file.

 

  • 使用MozillaCookieJar的代码是:
      import cookielib;
      gVal['cj'] = cookielib.MozillaCookieJar(localCookieFileName);
      
      #create cookie file
      gVal['cj'].save();
  • 得到cookie结果是:

      # Netscape HTTP Cookie File

      # http://www.netscape.com/newsref/std/cookie_spec.html

      # This is a generated file! Do not edit.

      .google.com TRUE / FALSE 1421304599 PREF ID=e33b271ab0f40036:FF=0:NW=1:TM=1358232599:LM=1358232599:S=6bX8PSZAJwtaGpi1

      .google.com.hk TRUE / FALSE 1374043799 NID 67=SQ5oTYjveXizHv3GNMOJx1gv0E87mTEAXlAHuqievzqo5CdD2yxmguzEErBcSJKUEG8pPege-JkD4PyiPGHltVF9bgTpjIvZc24nVZI9G5Fk1SRpSqsLk6DxDl-i4yj7

      .google.com.hk TRUE / FALSE 1421304599 PREF ID=9de55a3cd7314a09:U=c2e0eee9da3b56c4:FF=2:LD=zh-CN:NW=1:TM=1358232599:LM=1358232599:S=PclH1oPP1AIXo0Yl

 

【总结】

默认的是FileCookieJar没有实现save函数。

而MozillaCookieJar或LWPCookieJar都已经实现了。

所以可以用MozillaCookieJar或LWPCookieJar,去自动实现cookie的save。

实现,通过文件保存cookie。

建议用LWPCookieJar,其保存的cookie,易于人类阅读。

 

更多的,关于Python中的cookie的处理,可参考:

【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie

转载请注明:在路上 » 【已解决】Python中使用cookielib的FileCookieJar去save(),结果出错:NotImplementedError

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.182 seconds, using 22.12MB memory