【转】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 None, ValueError 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
【转】cookielib模块的更多相关文章
- python—cookielib模块对cookies的操作
最近用python写爬虫爬了点数据,确实是很好用的东西,今天对python如何操作cookie进行一下总结. python内置有cookielib模块操作cookie,配合urllib模块就可以了很轻 ...
- cookielib模块基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import cookielib #主要用于处理http客户端的co ...
- urllib2模块、cookielib模块
urllib2模块 urllib模块和urllib模块类似,用来打开URL并从中获取数据.与urllib模块不同的是,urllib模块不仅可以使用urlopen() 函数还可以自定义Opener来访问 ...
- cookielib模块 for python3
python2 可以直接安装cookielib模块 而py3却不能安装 故需要安装http模块 举例子: from http import cookiejar cookie = cookiejar.C ...
- Python3使用cookielib模块
同时使用过python2和python3的应该都知道,好多模块在python2中能直接安装,但是到了python3中却无法安装直接使用,同样python3中的好些模块在python2中也是一样 如下: ...
- Python cookielib 模块
什么是 cookie : 指某些网站为了辨别用户身份,进行 session 跟踪而储存在用户本地终端上的数据,通常以 txt 文件形式存储.比如你登录了淘宝,浏览器就会保存 cookie 信息,这样我 ...
- cookielib和urllib2模块相结合模拟网站登录
1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...
- cookielib和urllib2模块结合模拟网站登录
1.cookielib模块 cookielib模块的主要作用就是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问internet资源,例如可以利用本模块的cookiejar类的对 ...
- Cookielib
Cookielib模块主要的对象有CookieJar.FileCookieJar.MozillaCookieJar.LWPCookieJar 它们的关系:CookieJar —-派生—->Fil ...
随机推荐
- 快速失败机制--fail-fast
fail-fast 机制是Java集合(Collection)中的一种错误机制.当多个线程对同一个集合的内容进行操作时,就可能会产生fail-fast(快速失败)事件.例如:当某一个线程A通过iter ...
- for..of与for..in
var arr=[1,2,3,5] undefined for(var m of arr) console.log(m)//1,2,3,5 for(var m in arr) console.log( ...
- spring boot高性能实现二维码扫码登录(下)——订阅与发布机制版
前言 基于之前两篇(<spring boot高性能实现二维码扫码登录(上)——单服务器版>和<spring boot高性能实现二维码扫码登录(中)——Redis版>)的基础, ...
- 自动修改博客CSS样式用的代码
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js&qu ...
- PHP源代码加密
加密软件(php_screw) >下载网站:http://sourceforge.net/projects/php-screw/ >描述:php文件通常以文本格式存贮在服务器端, 很容易被 ...
- cisco交换机实现端口聚合
0x00前言: 今天听老师讲端口聚合,为了方便日后复习故此有 了本篇随笔. 0x01准备工具: cisco模拟器 0x02:目录 为什么要用端口聚合? 广播风暴? 扩展:SMTP 0x03正文: 为什 ...
- shiro权限框架(五)
五.与Spring集成 5.1 环境准备 <dependency> <groupId>org.apache.shiro</groupId> <artifact ...
- WEB页面异步调用场景测试
在我们测试异步调用前,我们首先弄清楚异步调用到底是什么? 异步调用的定义:一个可以无需等待被调用函数的返回值就让操作继续进行的方法, 举一个形象的例子就是:领导给A分配了一个任务, 然后领导就干其他事 ...
- Beta阶段敏捷冲刺每日报告——Day0
下一阶段需要改进完善的功能: 搜索框在Firefox和IE中显示不正常问题 下一阶段新增的功能: ToDoList功能:针对博主的功能,在博主登录之后可以添加和修改待办事项,每个事项包括标题.内容.日 ...
- TensorFlow问题“The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.”
出现的问题: 在使用TensorFlow跑官方教程例子时报以下warning: 虽程序能正常跑出结果,但作为一名强迫症患者对此很是不爽,于是查找资料找到隐藏该warning的解决办法. 解决办法: 在 ...