Document directory
- Urllib2
- Requests
- This is the urllib2 method:
- Requests
Ref: http://dancallahan.info/journal/python-requests/
Title: Useful Python Module
It is unnecessary to use python in HTTP-related processing, including the urllib2 module to obtain comprehensive functions at a huge cost of complexity. Compared with urllib2, the requests module of kenth Reitz supports Simple and simple use cases.
Simple Example:
Imagine that we tried to use the get method to retrieve resources from http://example.test/and then retrieve the code, content-typeheader information, and response's main body content. It is easy to implement either urllib2 or requests.
Urllib2
>>> import urllib2>>> url = 'http://example.test/'>>> response = urllib2.urlopen(url)>>> response.getcode()200>>> response.headers.getheader('content-type')'text/html; charset=utf-8'>>> response.read()'Hello, world!'
Requests
>>> import requests>>> url = 'http://example.test/'>>> response = requests.get(url)>>> response.status_code200>>> response.headers['content-type']'text/html; charset=utf-8'>>> response.contentu'Hello, world!'
These two methods are very similar. Compared with the urllib2 call method to read the attribute information in response, requests uses the attribute name to obtain the corresponding attribute value.
There are two subtle but important differences between the two:
1 requests automatically decodes the returned information with Unicode
2 requests automatically saves the returned content, so you can read it multiple times. Unlike urllib2.urlopen (), the returned content is only one object that can only be read once for a file type.
The second thing is that it is annoying to operate code in a python interactive environment.
A complex example:
Now let's try a complex example: Use the get method to get the resources of http://foo.test/secret. the basic httpverification of this example is as follows. Using the above Code as a template, it seems that we only need to replace the code between urllib2.urlopen () and requests. Get () with the username to send the password request.
This is the urllib2 method:
>>> import urllib2>>> url = 'http://example.test/secret'>>> password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()>>> password_manager.add_password(None, url, 'dan', 'h0tdish')>>> auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)>>> opener = urllib2.build_opener(auth_handler)>>> urllib2.install_opener(opener)>>> response = urllib2.urlopen(url)>>> response.getcode()200>>> response.read()'Welcome to the secret page!'
In a simple method, two classes are instantiated, and the third class is created. Finally, the urllib2 module is loaded to the global urllib2 module, and then the urlopen is called, so what are the two complex classes?
Confused, here all urllib2 document http://docs.python.org/release/2.7/library/urllib2.html
Then how does requests solve the same problem?
Requests
>>> import requests>>> url = 'http://example.test/secret'>>> response = requests.get(url, auth=('dan', 'h0tdish'))>>> response.status_code200>>> response.contentu'Welcome to the secret page!'
I added an auth keyword function when calling a method.
I bet you can remember it without checking the document.
Error Handling
Requests is also very useful in handling errors. If you use an incorrect user name and password, urllib2 will trigger a urllib2.urlerror error. However, requests will return a normal response object as expected. You only need to check the Boolean value of response. OK to know whether the login is successful.
>>> response = requests.get(url, auth=('dan', 'wrongPass'))>>> response.okFalse
Other features:
* Requests is as simple as the API of the head, post, put, patch, and delete methods.
* It can process multiple uploads and supports automatic transcoding.
* Better documentation
* More
Requests is good. You can try it next time you need to use HTTP.
Link:
Http://python-requests.org/
Http://pypi.python.org/pypi/requests