Python-cookielib [Urllib2/requests]

Source: Internet
Author: User

Cookie operation (read, add, delete, save), combined with URLLIB2 and requests for instructions, only to do a partial demonstration here, if in doubt, please refer to: (Author:nixawk)

Https://docs.python.org/2/library/cookielib.html
https://hg.python.org/cpython/file/2.7/Lib/cookielib.py

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

From Cookielib import Cookie
From Cookielib import Cookiejar
From Cookielib import Filecookiejar
From Cookielib import Lwpcookiejar
Import Urllib2
# Author:nixawk


def make_cookie (name, value, domain, path= '/'):
Return Cookie (version=0,
Name=name,
Value=value,
Port=none,
Port_specified=false,
Domain=domain,
Domain_specified=true,
Domain_initial_dot=false,
Path=path,
Path_specified=true,
Secure=false,
Expires=none,
Discard=false,
Comment=none,
Comment_url=none,
Rest=none,
Rfc2109=false)


def add_cookie (Cookiejar, cookie):
If Isinstance (Cookiejar, Cookiejar):
Cookiejar.set_cookie (Cookie)


def del_cookie (cookiejar, domain, Path, cookiename):
If Isinstance (Cookiejar, Cookiejar):
Cookiejar.clear (domain, path, cookiename)


def get_cookies_from_response (URL):
Cookiejar = Cookiejar ()

Opener = Urllib2.build_opener (
Urllib2. Httpcookieprocessor (Cookiejar))
Opener.open (URL)

# Add a new cookie or replace a old one
Newcookie = Make_cookie (' Newcookie ', ' 11111 ', '. baidu.com ', '/')

# Remove a cookie
Cookiejar.clear ('. baidu.com ', '/', ' Newcookie ')

Cookiejar.set_cookie (Newcookie)

Import PDB
Pdb.set_trace ()

Return Cookiejar


def save_cookies_to_file1 (URL):
Cookiefile = ' Cookies.log '

Filecookiejar = Filecookiejar (filename=cookiefile)

Opener = Urllib2.build_opener (
Urllib2. Httpcookieprocessor (Filecookiejar))
Opener.open (URL)

# *******************************************
# Please read code cookielib [Filecookiejar]

# Filecookiejar.save (filename=cookiefile)
# Raise Notimplementederror ()
# ******************************************

Try
Filecookiejar.save ()
Except Notimplementederror as E:
Print E

Return Filecookiejar


def save_cookies_to_file2 (URL):
Cookiefile = ' Cookies.log '

Lwpcookiejar = Lwpcookiejar (filename=cookiefile)

Opener = Urllib2.build_opener (
Urllib2. Httpcookieprocessor (Lwpcookiejar))
Opener.open (URL)

Lwpcookiejar.save ()

Return Lwpcookiejar

From Pprint import Pprint
Pprint (Get_cookies_from_response (' http://www.baidu.com '))

# Pprint (save_cookies_to_file1 (' http://www.baidu.com '))
# Pprint (save_cookies_to_file2 (' http://www.baidu.com '))

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

Import requests
# Author:nixawk


Def make_request (method, URL, headers={}, Files=none, data={},
Json=none, params={}, Auth=none,
Cookies=none, hooks={}):
Req = requests. Request (method, URL, headers={},
Files=none, data={},
Json=none, params={},
Auth=none,
Cookies=none, hooks={})

R = Req.prepare ()
s = requests. Session ()

Response = S.send (R)

return response


def get_cookies_from_response (response):
If hasattr (response, ' cookies '):
Return Response.Cookies

Return None


def save_cookies_to_file (URL):
Pass


From Pprint import Pprint

Response = make_request (' GET ', ' http://www.baidu.com/')
cookies = get_cookies_from_response (response)

Pprint (dir (cookies))


Python-cookielib [Urllib2/requests]

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.