http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/
#!/usr/bin/python#-*-coding:utf-8-*-"" "Function:" Organize "the processing of cookies in Python: Automatically process cookies, save as cookie files, load cookies from files/HTTP/ Www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file version:2013-01-15author:crifancontact: Admin (at) crifan.com "" "Import Os;import cookielib;import urllib2; Def pythonautohandlecookie (): "" "Demo how to Auto handle cookie in Python cookie in memory Cookies in File:save cookie to file LWP format Mozilla format Load cookie from File "" "Print" 1. Demo how to Auto handle cookies (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 would 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 cookies 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 would 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 cookies 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 would 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=jcfid2 XGXMIHPUPL #. google.com.hk true/false 1374047321 NID 67=klmi_z5zpwdjuyrwsuhie_kyi77_zijal0kwrougthagme86l KY7H-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 the cookie 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] "grooming" the processing of cookies in Python: Automatically processing cookies, saving them as cookie files, loading cookies from files