http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function: 【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file Version: 2013-01-15
Author: Crifan
Contact: admin (at) crifan.com
""" import os;
import cookielib;
import urllib2; def pythonAutoHandleCookie():
"""
Demo how to auto handle cookie in Python
cookies in memory
cookies in file:
save cookie to file
LWP format
Mozilla format
load cookie from file
""" print "1. Demo how to auto handle cookie (in memory)";
cookieJarInMemory = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarInMemory));
urllib2.install_opener(opener);
print "after init, cookieJarInMemory=",cookieJarInMemory; #after init, cookieJarInMemory= <cookielib.CookieJar[]>
#!!! following urllib2 will auto handle cookies
demoUrl = "http://www.google.com/";
response = urllib2.urlopen(demoUrl);
#here, we already got response cookies
print "after urllib2.urlopen, cookieJarInMemory=",cookieJarInMemory;
#after urllib2.urlopen, cookieJarInMemory= <cookielib.CookieJar[<Cookie PREF=ID=1e5d4d8621e61210:FF=0:NW=1:TM=1358235182:LM=1358235182:S=B-ONJ1lsQj5ARTEO for .google.com/>, <Cookie NID=67=lIY7YlPSrtQy4pHX_u8SrMAoG8-LXp8blElxXXfahe2ES3TVHzpaT3aejzaltwXetiE7TO7HrVwQCCe69tTh5K7Y5WyP8bvSZF20Myn1dcG7C780DNEeiT_QB0NeB4cR for .google.com.hk/>, <Cookie PREF=ID=4501e3b1f4a952b6:U=535aadd0e6b1070b:FF=2:LD=zh-CN:NW=1:TM=1358235182:LM=1358235182:S=Az7ZVYebAECtSKXd for .google.com.hk/>]> print "2. Demo how to auto handle cookie in file, LWP format";
cookieFilenameLWP = "localCookiesLWP.txt";
cookieJarFileLWP = cookielib.LWPCookieJar(cookieFilenameLWP);
#will create (and save to) new cookie file
cookieJarFileLWP.save();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarFileLWP));
urllib2.install_opener(opener);
#!!! following urllib2 will auto handle cookies
demoUrl = "http://www.google.com/";
response = urllib2.urlopen(demoUrl);
#update cookies, save cookies into file
cookieJarFileLWP.save();
#for demo, print cookies in file
print "LWP cookies:";
print open(cookieFilenameLWP).read(os.path.getsize(cookieFilenameLWP));
# #LWP-Cookies-2.0
# Set-Cookie3: PREF="ID=34c1415b570a93ae:FF=0:NW=1:TM=1358236121:LM=1358236121:S=gEVVojW4x37ht5n-"; path="/"; domain=".google.com"; path_spec; domain_dot; expires="2015-01-15 07:48:41Z"; version=0
# Set-Cookie3: NID="67=JI_uEwUm5GDrQ_vCwAp2z_YGU7MdLm5CLMa4CNLF7RQuTDMzrrk1EjRddGcnpoFbht81LaV9spxZQQInf0mPS6lDrvcRqBBL5NOTmy8SwOzA6HWC3iTIo4-o3fO1Udkv"; path="/"; domain=".google.com.hk"; path_spec; domain_dot; expires="2013-07-17 07:48:41Z"; HttpOnly=None; version=0
# Set-Cookie3: PREF="ID=8f7e4efca89bdb1b:U=f85a4afa4db021aa:FF=2:LD=zh-CN:NW=1:TM=1358236121:LM=1358236121:S=2WR59hDWutdnUJtF"; path="/"; domain=".google.com.hk"; path_spec; domain_dot; expires="2015-01-15 07:48:41Z"; version=0 print "3. Demo how to auto handle cookie in file, Mozilla Format";
cookieFilenameMozilla = "localCookiesMozilla.txt";
cookieJarFileMozilla = cookielib.MozillaCookieJar(cookieFilenameMozilla);
#will create (and save to) new cookie file
cookieJarFileMozilla.save();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarFileMozilla));
urllib2.install_opener(opener);
#!!! following urllib2 will auto handle cookies
demoUrl = "http://www.google.com/";
response = urllib2.urlopen(demoUrl);
#update cookies, save cookies into file
cookieJarFileMozilla.save();
#for demo, print cookies in file
print "Mozilla cookies:";
print open(cookieFilenameMozilla).read(os.path.getsize(cookieFilenameMozilla));
# # 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 1421308121 PREF ID=0e05040dd979207c:FF=0:NW=1:TM=1358236121:LM=1358236121:S=jcFid2XgXMIhPUPl
# .google.com.hk TRUE / FALSE 1374047321 NID 67=klMI_Z5ZPWDjUYrWSUHIE_kYI77_ziJaL0kWRoUGThagME86LKY7H-MNa2wAMI_GklIwYcD8t82qPinxzLd4GLDbmWT0OVLCXhRj0wQDC57dTNAsTs4lhVR7Yjvj2tfn
# .google.com.hk TRUE / FALSE 1421308121 PREF ID=028f8b736db06a9a:U=6ba6d080847c8de6:FF=2:LD=zh-CN:NW=1:TM=1358236121:LM=1358236121:S=_1BcC5v3G0ZojVz8 print "4. read cookies from file";
parseAndSavedCookieFile = "parsedAndSavedCookies.txt"
parsedCookieJarFile = cookielib.MozillaCookieJar(parseAndSavedCookieFile);
#parsedCookieJarFile = cookielib.MozillaCookieJar(cookieFilenameMozilla);
print parsedCookieJarFile; #<_MozillaCookieJar.MozillaCookieJar[]>
parsedCookieJarFile.load(cookieFilenameMozilla);
print parsedCookieJarFile; #<_MozillaCookieJar.MozillaCookieJar[<Cookie PREF=ID=5add236cafeb990c:FF=0:NW=1:TM=1358236707:LM=1358236707:S=9lhhp0W0zTCj8FVn for .google.com/>, <Cookie NID=67=Kx0fU67poTRN-ECBA_2zqr9KIUSP5a6DcGUefbD5R0ILsRAYSa109mAYIEF69LS40-UPrECtu756mH2nlz8mVneCVzANWfY7eFkmvQkeFVPGh9D58QYAeiFrUMed_OZB for .google.com.hk/>, <Cookie PREF=ID=442b8f2173d4249e:U=d01eca1334179f67:FF=2:LD=zh-CN:NW=1:TM=1358236707:LM=1358236707:S=_WQ1abARwIb5Crdj for .google.com.hk/>]> if __name__=="__main__":
pythonAutoHandleCookie();

  

[zz]【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie的更多相关文章

  1. 归纳整理Python中的控制流语句的知识点

    归纳整理Python中的控制流语句的知识点 Python 解释器在其最简单的级别,以类似的方式操作,即从程序的顶端开始,然后一行一行地顺序执行程序语句.例如,清单 1 展示了几个简单的语句.当把它们键 ...

  2. python中的目录递归的创建和目录及其下的文件递归的删除

    1,删除目录data下的train及其下的所有文件,相当于linux中的rm -rf import shutil shutil.rmtree('data/train') 2,递归的创建目录data/t ...

  3. python中的各个包的安装,用pip安装whl文件

    在安装之前,一直比较迷惑究竟如何用,安装完后,发现竟然如此简单 首先我一般用的是python27,官网下载python27msi安装window7 64位后,已经自带了Pip 和 easy_insta ...

  4. 如何将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

    cat /etc/issue|tr '[:lower:]' [:upper:] >> /tmp/issue.out

  5. Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据

    背景 Python中,想要打开已经存在的excel的xls文件,然后在最后新的一行的数据. 折腾过程 1.找到了参考资料: writing to existing workbook using xlw ...

  6. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  7. Python之路-Python中文件和异常

    一.文件的操作 open函数 在python中,使用open函数,打开一个已经存在的文件,或者新建一个新文件. 函数语法 open(name[, mode[, buffering[,encoding] ...

  8. Navicat的使用与python中使用MySQL的基本方法

    Navicat的使用与python中使用MySQL的基本方法 Navicat的下载及安装 下载地址 http://www.navicat.com.cn/download/navicat-premium ...

  9. Python中Cookie的处理(二)cookielib库

    Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持. 该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并 ...

随机推荐

  1. JSP页面无法使用EL导致"java.sql.SQLException: No suitable driver found for ${snapshot}"的问题

    使用JSTL来连接mysql,这个问题折腾了半天,老以为是Mysql驱动的问题,还好最后偶然发现了是EL表达式识别不了,报错: javax.servlet.ServletException: java ...

  2. 自测之Lesson4:gdb

    题目:列出gdb过程中常用的命令. 常用命令: 命令 作用 使用示例1 使用示例2 list 列出代码 list 行号 list 函数名 break 设置断点 break 行号 b 行号 run 运行 ...

  3. 【转载】【翻译】Breaking things is easy///机器学习中安全与隐私问题(对抗性攻击)

    原文:Breaking things is easy 译文:机器学习中安全与隐私问题(对抗性攻击) 我是通过Infaraway的那篇博文才发现cleverhans-blog的博客的,这是一个很有意思的 ...

  4. TCP系列14—重传—4、Karn算法和TSOPT的RTTM

    一.Karn算法 在RTT采样测量过程中,如果一个数据包初传后,RTO超时重传,接着收到这个数据包的ACK报文,那么这个ACK报文是对应初传TCP报文还是对应重传TCP报文呢?这个问题就是retran ...

  5. <Effective C++>读书摘要--Templates and Generic Programming<一>

    1.The initial motivation for C++ templates was straightforward: to make it possible to create type-s ...

  6. lol佐伊美图

      心血来潮,分享一波从各个网站上搜集到的佐伊美图,持续更新!(最近更新日期:2019/03/17) Section1 暮光星灵 2018/11/16 2019/02/15 2019/03/17 Se ...

  7. 【alpha】Scrum站立会议第3次....10.18

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app(约吧--暂定) 1.任务进度 2.燃尽图 功能列表 1.登录注册 2.创建跑步计划 3.筛选跑友 4.加一起跑步的人为好 ...

  8. 利用Vue v-model实现一个自定义的表单组件

    原文请点此链接  http://blog.csdn.net/yangbingbinga/article/details/61915038

  9. 【Docker 命令】- inspect命令

    docker inspect : 获取容器/镜像的元数据. 语法 docker inspect [OPTIONS] NAME|ID [NAME|ID...] OPTIONS说明: -f :指定返回值的 ...

  10. WIN7使用过360系统急救箱后出现的任务计划程序文件夹删除的办法

    直接进主题(怀疑系统有问题用了下360系统急救箱,用完后发现计划任务多了个360superkiller文件夹,右键直接是删除不了的) 尝试了各种方法都是不爽,突然想到计划任务不是在在系统盘下的一个文件 ...