How to use the Cookielib module in Python

Source: Internet
Author: User
Tags in python

Cookielib is a module that automatically handles cookies, and if we need to save cookies when using techniques such as reptiles, then cookielib will make you more effective! His most common partner module is urllib and request in Python.

However, the old high in the use of cookielib always encounter such a problem, in view of the cookielib source, some sentiment.

I. Core categories

Cookies

This class implements the cookie standard defined by the Netscape and RFC 2965 cookies, which can be understood as a single cookie data.

Part of the code is as follows, many properties are not familiar?

Self.domain_initial_dot = Domain_initial_dot
Self.path = Path
Self.path_specified = path_specified
Self.secure = Secure
Self.expires = Expires
Self.discard = Discard
Self.comment = Comment
Self.comment_url = Comment_url
self.rfc2109 = rfc2109
Cookiepolicy

The main function of this class is to send and receive cookies, that is, to ensure that the correct cookie is sent to the corresponding domain name, vice versa.

Defaultcookiepolicy

This class implements the Cookiepolicy interface.

Cookiejar

Cookiejar is a collection of cookies that can contain a lot of cookie classes and is our main object of action. There are a number of ways to support more detailed operation!

Filecookiejar

This class inherits from Cookiejar,cookiejar just completes its lifecycle in memory, Filecookiejar subclasses can implement data persistence, define save, load, revert three interfaces.

Mozillacookiejar & Lwpcookiejar

Two implementation classes, the inheritance relationship is as follows:

Mozillacookiejar & Lwpcookiejar

II. Use

Simple example

A simple code of use

#!/usr/bin/env python
# Encoding:utf-8


Import requests
Import Cookielib


url = ' http://www.baidu.com/'
Jar = Cookielib. Lwpcookiejar (' Cookie.txt ')

# Try to load cookies
# Ask a question, why do you want to add the Ignore_discard attribute?
Try
Jar.load (Ignore_discard=true)
Except
Pass

# Create a session
s = requests. Session ()

# Set Headers and cookies
S.headers = {' user-agent ': ' mozilla/5.0 ' (Macintosh; Intel Mac OS X 10_7_2) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.93 safari/537.36 '}
S.cookies = Jar

# Access URL
r = S.get (URL)

# Persistent Cookies
Jar.save (Ignore_discard=true)

# Print Cookies
For item in jar:
print ' Cookie name:%s----Value:%s '% (Item.name, Item.value)
We get the following cookie

Cat Cookie.txt

#LWP-cookies-2.0
Set-cookie3:baiduid= "2f5340b39928231aa09353cdae3da14d:fg=1"; Path= "/"; Domain= ". Baidu.com"; Path_spec; Domain_dot; expires= "2083-07-09 16:27:51z"; Version=0
set-cookie3:bidupsid=2f5340b39928231aa09353cdae3da14d; Path= "/"; Domain= ". Baidu.com"; Path_spec; Domain_dot; expires= "2083-07-09 16:27:51z"; Version=0
Set-cookie3:h_ps_pssid=14872_1457_14412_14509_14444_12826_10812_14430_12868_14871_12723_14962_14919_14902_ 15384_12095_13937_15963; Path= "/"; Domain= ". Baidu.com"; Path_spec; Domain_dot; Discard; Version=0
set-cookie3:pstm=1434892424; Path= "/"; Domain= ". Baidu.com"; Path_spec; Domain_dot; expires= "2083-07-09 16:27:51z"; Version=0
set-cookie3:bdsvrtm=0; Path= "/"; Domain= "www.baidu.com"; Path_spec; Discard; Version=0
set-cookie3:bd_home=0; Path= "/"; Domain= "www.baidu.com"; Path_spec; Discard; Version=0
Generate a Cookie class

We can simply generate a cookie by the definition of a cookie

Import Cookielib

Class Cookie:
def __init__ (self, version, name, value,
Port, Port_specified,
Domain, domain_specified, Domain_initial_dot,
Path, path_specified,
Secure
Expires
Discard,
Comment
Comment_url,
Rest
Rfc2109=false,
):
.....

# Initialization of a cookie

def createcookie (name, value, domain, Expires=none):
Return Cookielib. Cookies (
Version=none,
Name=name,
Value=value,
Port= ' 80 ',
Port_specified=true,
Domain=domain,
Domain_specified=true,
Domain_initial_dot=false,
Path= '/',
Path_specified=true,
Secure=false,
Expires=expires,
Discard=false,
Comment=none,
Comment_url=none,
Rest=none,
Rfc2109=false
)


New_cookie = Createcookie (' Phpgao ', ' Laogao ', ' www.phpgao.com ', ' 1434977736 ')

# Add to Existing cookies

MyCookie = Cookielib. Cookiejar ()
Mycookie.set_cookie (New_cookie)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.