【轉】cookielib模塊


cookielib模塊

      cookielib模塊的主要作用是提供可存儲cookie的對象,以便於與urllib2模塊配合使用來訪問Internet資源。例如可以利用本模塊 的CookieJar類的對象來捕獲cookie並在后續連接請求時重新發送。coiokielib模塊用到的對象主要有下面幾個:CookieJar、 FileCookieJar、MozillaCookieJar、LWPCookieJar。其中他們的關系如下:

                        CookieJar                       

                            /    

            FileCookieJar      

             /                   \    

 MozillaCookieJar      LWPCookieJar            

 

1、CookieJar ()

      管理HTTP cookie值、存儲HTTP請求生成的cookie、向傳出的HTTP請求添加cookie的對象。整個cookie都存儲在內存中,對CookieJar實例進行垃圾回收后cookie也將丟失。

      The CookieJar class stores HTTP cookies. It extracts cookies from HTTP requests, and returns them in HTTP responses.CookieJar instances automatically expire contained cookies when necessary. Subclasses are also responsible for storing and retrieving cookies from a file or database.

2、FileCookieJar (filename,delayload=None,policy=None)

       創建FileCookieJar實例,檢索cookie信息並將cookie存儲到文件中。filename是存儲cookie的文件名。delayload為True時支持延遲訪問訪問文件,即只有在需要時才讀取文件或在文件中存儲數據。

      A CookieJar which can load cookies from, and perhaps save cookies to, a file on disk. Cookies are NOT loaded from the named file until either the load() or revert() method is called.

3、MozillaCookieJar (filename,delayload=None,policy=None)

      創建與Mozilla瀏覽器cookies.txt兼容的FileCookieJar實例。

       A FileCookieJar that can load from and save cookies to disk in the Mozilla  cookies.txt  file format (which is also used by the Lynx and Netscape browsers).Also note that cookies saved while Mozilla is running will get clobbered by Mozilla.

4、LWPCookieJar (filename,delayload=None,policy=None)

      創建與libwww-perl的Set-Cookie3文件格式兼容的FileCookieJar實例。

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

  

     除了上面幾個函數之外,下面幾個函數也很重要:

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 NoneValueError is raised. ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expiredThe 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.

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

Load cookies from a file.Old cookies are kept unless overwritten by newly loaded ones.Arguments are as for save().The named file must be in the format understood by the class, or LoadError will be raised. Also, IOError may be raised, for example if the file does not exist.

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

Clear all cookies and reload cookies from a saved file. revert() can raise the same exceptions as load(). If there is a failure, the object’s state will not be altered.

cookielib模塊一般與urllib2模塊配合使用,主要用在urllib2.build_oper()函數中作為urllib2.HTTPCookieProcessor()的參數。使用方法如下面登錄人人網的代碼:

復制代碼
#! /usr/bin/env python #coding=utf-8 import urllib2 import urllib import cookielib data={"email":"用戶名","password":"密碼"} #登陸用戶名和密碼 post_data=urllib.urlencode(data) cj=cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) headers ={"User-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"} req=urllib2.Request("http://www.renren.com/PLogin.do",post_data,headers) content=opener.open(req) print content2.read().decode("utf-8").encode("gbk")

來源:http://www.cnblogs.com/mmix2009/p/3226775.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM