1.将cookies保存到变量中,然后打印cookie中的值

#coding:utf-8
#将cookies保存到变量中,然后打印cookie中的值
import urllib2
import cookielib
#声明一个CookieJar对象实例保存cookie
cookie=cookielib.CookieJar()
#利用urllib2库中的HTTPCookieProcessor对象来创建cookie处理器
handler=urllib2.HTTPCookieProcessor(cookie)
#通过handler来构建opener
opener=urllib2.build_opener(handler) re=opener.open('https://www.baidu.com/')
for sys in cookie:
print 'Name='+sys.name
print 'Value='+sys.value

2.保存cookies到文件

#coding:utf-8
#保存cookies到文件
import urllib2
import cookielib
import requests #设置保存cookie的文件,同级目录下的cookie.txt
filename='cookies.txt'
#声明一个MozillaCookieJar对象实例来保存cookie,之后写入文件
cookie=cookielib.MozillaCookieJar(filename)
#利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
hander=urllib2.HTTPCookieProcessor(cookie)
#通过handler来构建opener
opener=urllib2.build_opener(hander)
re=opener.open('https://www.baidu.com/')
#response=opener.open('https://www.baidu.com/')
#保存cookies到文件
cookie.save(ignore_discard=True,ignore_expires=True)

3. 运用cookies模拟登录

#coding:utf-8
import urllib2
import urllib
import cookielib filename='cookies.txt'
cookie=cookielib.MozillaCookieJar(filename)
hander=urllib2.HTTPCookieProcessor(cookie)
opener=urllib2.build_opener(hander) data=urllib.urlencode({'j_username':'admin',
'j_password':'xxxxxxx'})
loginUrl='https://www.baidu.com'
re=opener.open(loginUrl,data)
cookie.save('cookies.txt',ignore_discard=True, ignore_expires=True)
url='https://www.baidu.com' result=opener.open(url)
print result.read()

  


python_cookies的更多相关文章

随机推荐

  1. APACHE + LDAP 的权限认证配置方法

    原文地址:http://www.chinaunix.net/jh/49/627646.html 一.前言 很多朋友希望利用 Apache 通过 LDAP 进行用户认证及权限管理.     通过多次试验 ...

  2. Linux下将/TMP和/Var移动到共享分区

    2007-03-09 03:25:08    整理数据 首先,必须创建一个新分区专门用于存储频繁修改的文件.您可能希望将这个分区置于单独的磁盘上以增强性能.接下来,我将逐步说明将 /tmp 和 /va ...

  3. 乱码字符引起的JSON转换失败

        这种问题有两个解决办法,使用后不管你是什么特殊字符还是什么西欧字体还是什么稀奇古怪的内容都可以完美解决!   1.要把即将转换为json的内容先转换为unicode编码,json转换无误后,使 ...

  4. Axure 蚂蚁设计团队组件库 让交互稿美美"搭"

    Github资源:https://github.com/ant-design/ant-design-pro English | 简体中文 技术实践篇 https://pro.ant.design/do ...

  5. tp模型和数据库操作方法

    一.新建的模型名和表名一样,采用驼峰式,如表名user_type模型取名为UserType namespace app\index\model;use think\Model;class UserTy ...

  6. java程序猿常用Linux命令

    1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...

  7. mysql date and time type ---- mysql 时间&日期 类型详解

    mysql 中支持用多种方式来表示时间与日期 一.mysql 中能表示时间与日期的数据类型: 1.表示年 ) -- 最好不要用这个数据类型.对于年份的取值中有[1901 --> 2155] + ...

  8. Sql UNION 合并多个结果集并排序

    1.建表语句及测试数据: --创建表A CREATE TABLE A( A1 int NULL, A2 nvArchAr(50) NULL, A3 decimAl(18, 0) NULL ) --测试 ...

  9. ubuntu下将CapsLock改为Ctrl键

    需求:Ubuntu下用Vim时,ESC因为在左上角,还算是好按,但是Ctrl就太坑了,在左右两个下角,实在是太不方便了. 经过分析决定将:CapsLock键改为Ctrl,但仍然保留下面的原Ctrl键( ...

  10. Flume 中文入门手冊

    原文:https://cwiki.apache.org/confluence/display/FLUME/Getting+Started 什么是 Flume NG? Flume NG 旨在比起 Flu ...