Processing of Cookies in Python (2) cookielib library

黄舟
Release: 2016-12-23 17:31:30
Original
1769 people have browsed it

The cookielib library in Python (http.cookiejar in python3) provides client-side support for storing and managing cookies.

The main function of this module is to provide objects that can store cookies. Use this module to capture cookies and resend them on subsequent connection requests. It can also be used to process files containing cookie data.

This module mainly provides these objects, CookieJar, FileCookieJar, MozillaCookieJar, LWPCookieJar.

1. CookieJar

CookieJar objects are stored in memory.

>>> import urllib2
>>> import cookielib
>>> cookie=cookielib.CookieJar()
>>> handler=urllib2.HTTPCookiePRocessor(cookie)
> ;>> opener=urllib2.build_opener(handler)
>>> opener.open('http://www.google.com.hk')

The cookie for accessing google has been captured, Let’s see what it looks like:

>>> print cookie
, < ;Cookie PREF=ID=7ae0fa51234ce2b1:FF=0:NW=1:TM=1391219446:LM=1391219446:S=cFiZ5X8ts9NY3cmk for .google.com.hk/>]>

It seems to be a collection of Cookie instances , Cookie instances have attributes such as name, value, path, expires:

>>> for ck in cookie:
... Print ck.name,':',ck.value
...
NID: 67=B6YQoEIEjcqDj-adada_WmNYl_JvADsDEDchFTMtAgERTgRjK452ko6gr9G0Q5p9h1vlmHpCR56XCrWwg1pv6iqhZnaVlnwoeM-Ln7kIUWi92l-X2fvUqgwDnN3qowDW
PREF: ID=7ae0 fa51234ce2b1:FF=0:NW=1:TM=1391219446:LM=1391219446:S=cFiZ5X8ts9NY3cmk

2. Capture cookie to file

FileCookieJar(filename )

Create a FileCookieJar instance, retrieve cookie information and store the information into a file, filename is the file name.

MozillaCookieJar(filename)

Creates a FileCookieJar instance compatible with the Mozilla cookies.txt file.

LWPCookieJar(filename)

Creates a FileCookieJar instance compatible with the libwww-perl Set-Cookie3 file. I code: m 2 Import Urllib2

3 Import Cookielib

4 DEF HANDLECOOKIE ():

5

6 #handle cook file
7 filename = 'filecookiejar.txt'
8 URL =' http:/ /www.google .com.hk'
9 FileCookieJar=cookielib.LWPCookieJar(filename)
10 FileCookeJar.save()
11 opener =urllib2.build_opener(urllib2.HTTPCookieProcessor(FileCookieJar))
12 opener.open(url)
13 FileCookieJar.save ()
14 print open(filename).read()
15
16 #read cookie from file
17 readfilename = "readFileCookieJar.txt"
18 MozillaCookieJarFile =cookielib.MozillaCookieJar()
19 print Mozilla CookieJarFile
20 MozillaCookieJarFile.load( readfilename)
21 Print MozillaCookieJarFile
22 if __name__=="__main__":
23 HandleCookie()


The above is the processing of cookies in Python (2) The content of the cookielib library. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template