设为首页 收藏本站
查看: 1111|回复: 0

[经验分享] 【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie

[复制链接]

尚未签到

发表于 2015-12-15 10:52:22 | 显示全部楼层 |阅读模式
文章为转载,原文链接:http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/

1、Python中的Cookie相关的库
Python中有自带的模块:cookielib
可以处理cookie相关的很多事情。
包括

  • 从文件中载入cookie
更多详细格式和解释,请直接看下面的代码。

2、Python中自动处理Cookie,将Cookie保存到文件,从文件中读取Cookie

Python代码:




  • #!/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=
  •     #!!! 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=


  •     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   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; #
  •     parsedCookieJarFile.load(cookieFilenameMozilla);
  •     print parsedCookieJarFile; #


  • if __name__=="__main__":
  •     pythonAutoHandleCookie();

3、总结

Python中的cookie的自动处理,比C#好多了。
至少有已有的库可供使用。虽然用法上,需要稍微多多了解后,才知道如何使用的。

更多的解释,可参考官方手册:
cookielib — Cookie handling for HTTP clients
比如其中的revert等函数,需要用到的时候,应该会觉得蛮好用的。

原帖的cookie保存的时候并没有获得全部的cookie,后来又找另外一个文档,对save函数加了两个函数:
ckjar.save(ignore_discard=True, ignore_expires=True)

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.
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-151438-1-1.html 上篇帖子: Python改写的查看股票实时行情的代码 下篇帖子: Python初学者(零基础学习Python、Python入门)常见问题:书籍推荐、资料、社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表