The Filecookiejar of Reptiles

Source: Internet
Author: User

Brief introduction

Although the Cookiejar module is capable of setting a cookie for a request, its cookie is stored in memory and needs to be reset each time.

This derives a subclass of it---filecookiejar, which can store the cookie in a file and then use it directly.

Save the sample cookie
From urllib import request,parsefrom http import cookiejar# Create Filecookiejar Instance object # It takes a parameter, both the location of the cookie to save the filename = ' Cookie.txt ' cookie = Cookiejar. Filecookiejar (FileName) # the manager Cookie_handle = Request that generated the cookie based on the cookie created. Httpcookieprocessor (Cookie) # Create HTTP request Manager Http_handle = Request. HttpHandler () # Create HTTPS manager Https_handle = Request. Httpshandler () # Create a request manager, with the above 3 managers as parameter Properties # with opener, you can replace Urlopen to get the requested opener = Request.build_opener (Cookie_handle, HTTP_HANDLE,HTTPS_HANDLE) def login (): "The first login required to pass the user name and password, to obtain the login cookie voucher ' # login URL, from the login form of the Action property is obtained        Fetch URL = ' http://www.renren.com/PLogin.do ' # data required for login, data in dictionary form, # This key value needs to get data from the name attribute of the corresponding input in the form's shoulder pole ({     ' Email ': ' [email protected] ', ' Password ': ' 123456 '} # data is parsed into UrlEncode format req = Request. Request (Url,data=data) # Normal is with Request.urlopen (), here with Opener.open () to initiate requests response = Opener.open (req) # Save cookie file C Ookie.save () if __name__ = = ' __main__ ': ' Execute login LetterCount "Login () 

  

Invocation of a cookie
From urllib import request,parsefrom http import cookiejar# Create Cookiejar instance Object cookie = Cookiejar. Filecookiejar () # Read the saved cookie file # After reading, no need to login, direct access to the home page can cookie.load (' Cookie.txt ') # based on the cookie created by the manager to generate a cookie Cookie_ handle = Request. Httpcookieprocessor (Cookie) # Create HTTP request Manager Http_handle = Request. HttpHandler () # Create HTTPS manager Https_handle = Request. Httpshandler () # Create a request manager, with the above 3 managers as parameter Properties # with opener, you can replace Urlopen to get the requested opener =  Request.build_opener (cookie_ Handle,http_handle,https_handle) def gethomepage ():    '    get login after page    ' '    # This URL is the URL of the link address after login    = ' Http://www.renren.com/965187997/profile '    # if the login function above has been executed,    # then opener is already a opener object containing the cookie information    res = opener.open (URL)    html = Res.read (). Decode ()    with open (' renren.html ', ' W ') as F:        f.write (HTML) if __name__ = = ' __main__ ':    gethomepage ()

  

The Filecookiejar of Reptiles

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.