2015.3.6
The use of URLLIB2 is roughly as follows # Custom handler processing function opener = Urllib2.build_opener (Proxyhandler, HttpHandler) Urllib2.install_opener ( Opener) # custom URL parameter request = Urllib2. Request () request.add_headers (XXX) # Open URL, return file-like object response = Urllib2.urlopen (req) # Get results Print response.read () Analysis of key processing mechanisms
Urlopen ()
Is the entry function for processing.
Gets the Openerdirector object, calling Opener.open ().
The default Openerdirector object is saved in the variable _open.
The singleton mode is used.
Build_opener ()is an object factory function that creates a Openerdirector object. The default handler list is provided internally, and the function passes in the handler parameter instead of the default handlers. Call the Openerdirector.add_handler () function to register the handlers. Register the default handler first, then register the custom handler. Register according to the order in the parameters.
Install_opener ()Saves the Openerdirector object to the variable _opener, which is used as the default opener.
class OpenerdirectorThe composite mode is used. Add_handler (): Registers the handler object into the internal list. Open (): Invokes the Open method of each object in the handler list, triggering the processing operation.
class RequestAs an information object, save and URL related parameters, including headers, data, proxy and so on. Used for the delivery of URL parameters.
class HttpHandlerClass inheritance relationship: Basehandler, AbstracthttphandlerHttpHandlerCall Httplib. Httpconnection completes the HTTP processing.
Python urllib2 Module Learning notes