[Python] web crawler (4): Opener, Handler, and openerhandler
Before proceeding, let's first explain the two methods in urllib2: info and geturl.
The response object response (or HTTPError instance) returned by urlopen has two useful methods: info () and geturl ()
1. geturl ():
Geturl () returns the obtained real URL, which is useful because urlopen (or the opener object) may be redirected. The obtained URL may be different from the requested URL.
Take a hyperlink from a person as an example,
We will create a urllib2_test10.py file to compare the original URL with the redirection link:
[Python]View plaincopy
- 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 the command, you can see the URL that the real link points:
2. info ():
Info () returns the dictionary object of the object, which describes the page information obtained. It is usually the specific headers sent by the server.
The classic headers include "Content-length", "Content-type", and other Content.
We will create a urllib2_test11.py to test the info application:
[Python]View plaincopy
- 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 running result is as follows:
The following describes two important concepts in urllib2: Openers and Handlers.
I. opener
The urllib2.urlopen () function does not support authentication, cookie, or other advanced HTTP functions. To support these functions, you must use build_opener () (which can be used to allow python programs to simulate browser access, so you can understand it ~) Function to create a custom Opener object.
Usage:
build_opener([handler1 [ handler2, ... ]])
Handler is a Handler instance. HTTPBasicAuthHandler, HTTPCookieProcessor, and ProxyHandler are commonly used.
The objects returned by build_opener () have the open () method, which is the same as the functions of the urlopen () function.
To modify the http header, you can use:
import urllib2opener = urllib2.build_opener()opener.addheaders = [('User-agent', 'Mozilla/5.0')]opener.open('http://www.example.com/')
Install_opener (opener) installs different opener objects as the global opener used by urlopen.
The HTTPBasicAuthHandler () handler can use add_password () to set the password.
h.add_password(realm,uri,user,passwd)
Realm is the name or description associated with the verification, depending on the remote server. Uri is the base URL. User and passwd specify the user name and password respectively.
import urllib2auth=urllib2.HTTPBasicAuthHandler()auth.add_password('Administrator','http://www.example.com','Dave','123456')opener=urllib2.build_opener(auth)u=opener.open('http://www.example.com/evilplan.html')
Cookie processing (HTTPCookieProcessor)
import urllib2,cookielibcookie=cookielib.CookieJar()cookiehand=urllib2.HTTPCookieProcessor(cookie)opener=urllib2.build_opener(cookiehand)
Proxy (ProxyHandler)
The ProxyHandler (proxies) parameter proxies is a dictionary that maps protocol names (http, ftp) and so on to the URL of the corresponding proxy server.
proxy=ProxyHandler({'http':'http://someproxy.com:8080'})auth=HTTPBasicAuthHandler()auth.add_password()opener=build_opener(auth,proxy)
You can also use a proxy in urlopen.
import urllib2 proxy = 'http://%s:%s@%s' % ('userName', 'password', 'proxy') inforMation = urllib2.urlopen("http://www.example.com", proxies={'http':proxy})
2. Handler
Handler is used to process URLs, such as HTTP redirection and HTTP cookies.
If you want to create a specific openers, such as obtaining an opener that can process cookies or obtaining an opener without redirection, you need to use a Custom handler.
Basic Authentication: In order to demonstrate how to create and install a handler, we will use HTTPBasicAuthHandler.
During verification, the server sends a header (401 error code) request for verification. Scheme and a 'realm' are specified. The format is www-authenticate: SCHEME realm = "realm ".
For example: www-authenticate: Basic realm = "cPanel Users"
The client must use a new request and contain the correct name and password in the request header.
To simplify this process, we can create an HTTPBasicAuthHandler instance and let opener use this handler.
HTTPBasicAuthHandler uses a password management object to process URLs and realms to map user names and passwords.
If you know what realm is, you can use HTTPPasswordMgr.
If you do not care about what realm is, you can use HTTPPasswordMgrWithDefaultRealm.
The Code is as follows:
[Python]View plaincopy
- #-*-Coding: UTF-8 -*-
- Import urllib2
- # Create a password management
- Password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm ()
- # Add a user name and password
- Top_level_url = "http://example.com/foo"
- Password_mgr.add_password (None, top_level_url, 'why', '123 ')
- # Create a new handler
- Handler = urllib2.HTTPBasicAuthHandler (password_mgr)
- # Create an "opener" (OpenerDirector instance)
- Opener = urllib2.build _ opener (handler)
- A_url = 'HTTP: // www.baidu.com /'
- # Use opener to obtain a URL
- Opener. open (a_url)
- # Install opener.
- # Now all calls to urllib2.urlopen will use our opener.
- Urllib2.install _ opener (opener)
Note: In addition to HHTPBasicAuthHandler, ProxyHandler, UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, and HTTPErrorProcessor all return Handler.
Welcome to my public account
Zookeeper