0 Basic Writing Python crawler urllib2 two important concepts: openers and handlers

Source: Internet
Author: User
Before we start, let's explain two methods in Urllib2: Info/geturl
Response (or Httperror instance) of the reply object returned by Urlopen has two useful methods, info () and Geturl ()

1.geturl ():

This returns the actual URL obtained, which is useful because Urlopen (or opener objects used) may have redirects. The URL you get may be different from the request URL.
Take a hyperlink from everyone, for example,
Let's build a urllib2_test10.py to compare the original URL with the redirected link:

Copy the Code code as follows:


From URLLIB2 import Request, Urlopen, Urlerror, Httperror
Old_url = ' Http://rrurl.cn/b1UZuP '
req = Request (old_url)
Response = Urlopen (req)
print ' old URL: ' + old_url
print ' Real URL: ' + response.geturl ()

After running you can see the URL that the true link points to:

2.info ():

The Dictionary object that returns the object that describes the page condition that was obtained. Typically, the server sends a specific header headers. It is now httplib. Httpmessage instance.
The classic headers contains "Content-length", "Content-type", and other content.
Let's build a urllib2_test11.py to test the application of info:

Copy the Code code as follows:


From URLLIB2 import Request, Urlopen, Urlerror, Httperror
Old_url = ' http://www.baidu.com '
req = Request (old_url)
Response = Urlopen (req)
print ' Info (): '
Print Response.info ()

The results of the run are as follows, and you can see information about the page:

Here are two important concepts in URLLIB2: openers and handlers.

1.Openers:

When you get a URL you use a opener (a urllib2. Openerdirector instances).
Under normal circumstances, we use the default opener: through Urlopen.
But you can create a personality openers.

2.Handles:

Openers uses processor handlers, all "heavy" work is handled by handlers.
Each handlers knows how to open URLs through a specific protocol, or how to handle various aspects of the URL when it is opened.
such as HTTP redirection or HTTP cookies.

If you want to get URLs with a specific processor, you'll want to create a openers, such as a opener that can handle cookies, or a opener that doesn't redirect.

To create a opener, you can instantiate a openerdirector,
Then call. Add_handler (some_handler_instance).
Similarly, you can use Build_opener, which is a more convenient function for creating opener objects, and he only needs one function call at a time.
Build_opener adds several processors by default, but provides a quick way to add or update the default processor.
Other processor handlers you might want to handle proxies, validations, and other common but somewhat special situations.

Install_opener is used to create a (global) default opener. This indicates that calling Urlopen will use the opener you installed.
The opener object has an open method.
This method can be used to obtain URLs as directly as the Urlopen function: it is not usually necessary to call Install_opener, except for convenience.

After saying the above two things, let's take a look at the basic certification content, here will use the above mentioned opener and handler.

Basic Authentication Fundamental Validation

In order to demonstrate the creation and installation of a handler, we will use Httpbasicauthhandler.
When basic authentication is required, the server sends a header (401 error code) to request validation. This specifies SCHEME and a ' realm ', which looks like this: Www-authenticate:scheme realm= "Realm".
For example
Www-authenticate:basic realm= "CPanel Users"
The client must use the new request and include the correct name and password in the request header.
This is "basic verification", in order to simplify this process, we can create an instance of Httpbasicauthhandler, and let opener use this handler.

Httpbasicauthhandler uses a password-managed object to process URLs and realms to map user names and passwords.
If you know what realm is (from the server), you can use Httppasswordmgr.

Often people don't care what realm is. In that case, you can use the convenient httppasswordmgrwithdefaultrealm.
This will specify a default user name and password for the URL.
This will be provided when you provide a different combination for a particular realm.
We indicate this by specifying none for the realm parameter to be provided to Add_password.

The highest-level URL is the first one that requires validation. The more profound URLs you pass on to. Add_password () will be equally appropriate.
Having said so much nonsense, here's an example to illustrate what is said above.
Let's build a urllib2_test12.py to test the application of info:

Copy the Code code as follows:


#-*-Coding:utf-8-*-
Import Urllib2
# Create a password manager
Password_mgr = Urllib2. Httppasswordmgrwithdefaultrealm ()
# Add user name and password
Top_level_url = "http://example.com/foo/"
# If we know realm, we can use him instead of ' None '.
# Password_mgr.add_password (None, top_level_url, username, password)
Password_mgr.add_password (None, Top_level_url, ' Why ', ' 1223 ')
# Create a new handler
Handler = Urllib2. Httpbasicauthhandler (Password_mgr)
# create "opener" (Openerdirector instance)
Opener = Urllib2.build_opener (handler)
A_url = ' http://www.baidu.com/'
# Get a URL using opener
Opener.open (A_url)
# Install opener.
# now all calls to Urllib2.urlopen will be used by our opener.
Urllib2.install_opener (opener)



Note: The above example we only provide our hhtpbasicauthhandler to Build_opener.
The default openers has a normal condition of handlers:proxyhandler,unknownhandler,httphandler,httpdefaulterrorhandler, HTTPRedirectHandler, Ftphandler, Filehandler, Httperrorprocessor.
The Top_level_url in the code can actually be a full URL (containing "http:" and a host name and an optional port number).
For example: http://example.com/.
It can also be a "authority" (that is, host name and optional include port number).
For example: "example.com" or "example.com:8080".
The latter contains the port number.
  • 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.