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