【整理】Python中Cookie的处理

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


1

Python中的Cookie相关的库

Python中有自带的模块:cookielib

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

包括

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

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

 

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

Python代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:   【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie
 
 
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
     # # 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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值