Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97663
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-11 22:44
个人简介

HUST16届准毕业生,发奋求职中...

文章分类

全部博文(21)

文章存档

2015年(17)

2014年(4)

我的朋友

分类: Python/Ruby

2015-05-14 13:49:32

文章为转载,原文链接:

1、Python中的Cookie相关的库

Python中有自带的模块:

可以处理cookie相关的很多事情。

包括

  • 自动处理cookie:cookie数据是保存在内存中
  • 自动处理cookie:支持Cookie数据存放到文件中
    • 其中有两种格式:
      • LWP
      • Mozilla
    • 进一步,再支持:
      • 将cookie存到文件里面
      • 从文件中载入cookie

更多详细格式和解释,请直接看下面的代码。

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

Python代码:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Function: 【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie
  5.             

  6. Version: 2013-01-15
  7. Author: Crifan
  8. Contact: admin (at) crifan.com
  9. """

  10. import os;
  11. import cookielib;
  12. import urllib2;

  13. def pythonAutoHandleCookie():
  14.     """
  15.         Demo how to auto handle cookie in Python
  16.             cookies in memory
  17.             cookies in file:
  18.                 save cookie to file
  19.                     LWP format
  20.                     Mozilla format
  21.                 load cookie from file
  22.     """

  23.     print "1. Demo how to auto handle cookie (in memory)";
  24.     cookieJarInMemory = cookielib.CookieJar();
  25.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarInMemory));
  26.     urllib2.install_opener(opener);
  27.     print "after init, cookieJarInMemory=",cookieJarInMemory; #after init, cookieJarInMemory= <cookielib.CookieJar[]>
  28.     #!!! following urllib2 will auto handle cookies
  29.     demoUrl = "";
  30.     response = urllib2.urlopen(demoUrl);
  31.     #here, we already got response cookies
  32.     print "after urllib2.urlopen, cookieJarInMemory=",cookieJarInMemory;
  33.     #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/>]>
  34.     
  35.     
  36.     print "2. Demo how to auto handle cookie in file, LWP format";
  37.     cookieFilenameLWP = "localCookiesLWP.txt";
  38.     cookieJarFileLWP = cookielib.LWPCookieJar(cookieFilenameLWP);
  39.     #will create (and save to) new cookie file
  40.     cookieJarFileLWP.save();
  41.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarFileLWP));
  42.     urllib2.install_opener(opener);
  43.     #!!! following urllib2 will auto handle cookies
  44.     demoUrl = "";
  45.     response = urllib2.urlopen(demoUrl);
  46.     #update cookies, save cookies into file
  47.     cookieJarFileLWP.save();
  48.     #for demo, print cookies in file
  49.     print "LWP cookies:";
  50.     print open(cookieFilenameLWP).read(os.path.getsize(cookieFilenameLWP));
  51.     # #LWP-Cookies-2.0
  52.     # 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
  53.     # 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
  54.     # 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

  55.     print "3. Demo how to auto handle cookie in file, Mozilla Format";
  56.     cookieFilenameMozilla = "localCookiesMozilla.txt";
  57.     cookieJarFileMozilla = cookielib.MozillaCookieJar(cookieFilenameMozilla);
  58.     #will create (and save to) new cookie file
  59.     cookieJarFileMozilla.save();
  60.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJarFileMozilla));
  61.     urllib2.install_opener(opener);
  62.     #!!! following urllib2 will auto handle cookies
  63.     demoUrl = "";
  64.     response = urllib2.urlopen(demoUrl);
  65.     #update cookies, save cookies into file
  66.     cookieJarFileMozilla.save();
  67.     #for demo, print cookies in file
  68.     print "Mozilla cookies:";
  69.     print open(cookieFilenameMozilla).read(os.path.getsize(cookieFilenameMozilla));
  70.     # # Netscape HTTP Cookie File
  71.     # # http://www.netscape.com/newsref/std/cookie_spec.html
  72.     # # This is a generated Do not edit.

  73.     # .google.com    TRUE    /    FALSE    1421308121    PREF    ID=0e05040dd979207c:FF=0:NW=1:TM=1358236121:LM=1358236121:S=jcFid2XgXMIhPUPl
  74.     # .google.com.hk    TRUE    /    FALSE    1374047321    NID    67=klMI_Z5ZPWDjUYrWSUHIE_kYI77_ziJaL0kWRoUGThagME86LKY7H-MNa2wAMI_GklIwYcD8t82qPinxzLd4GLDbmWT0OVLCXhRj0wQDC57dTNAsTs4lhVR7Yjvj2tfn
  75.     # .google.com.hk    TRUE    /    FALSE    1421308121    PREF    ID=028f8b736db06a9a:U=6ba6d080847c8de6:FF=2:LD=zh-CN:NW=1:TM=1358236121:LM=1358236121:S=_1BcC5v3G0ZojVz8
  76.     
  77.     print "4. read cookies from file";
  78.     parseAndSavedCookieFile = "parsedAndSavedCookies.txt"
  79.     parsedCookieJarFile = cookielib.MozillaCookieJar(parseAndSavedCookieFile);
  80.     #parsedCookieJarFile = cookielib.MozillaCookieJar(cookieFilenameMozilla);
  81.     print parsedCookieJarFile; #<_MozillaCookieJar.MozillaCookieJar[]>
  82.     parsedCookieJarFile.load(cookieFilenameMozilla);
  83.     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/>]>

  84.     
  85. if __name__=="__main__":
  86.     pythonAutoHandleCookie();

3、总结

Python中的cookie的自动处理,比C#好多了。

至少有已有的库可供使用。虽然用法上,需要稍微多多了解后,才知道如何使用的。

 

更多的解释,可参考官方手册:

比如其中的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.


阅读(1864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~