Original link :http://www.cnblogs.com/yuxc/
As a Python rookie, has been ignorant of urllib and urllib2, thought 2 is 1 of the upgraded version. Today saw a foreigner wrote "Python:difference between Urllib and Urllib2" only understand the difference.
You might is intrigued by the existence of the separate URLs modules in Python- urllib
and urllib2
. Even more intriguing:they is not alternatives for each other. So what's the difference between urllib
urllib2
and, and do we need them both?
urllib
and
urllib2are Both Python modules this do URL request Related stuff but offer different functionalities. Their significant differences is listed below:
urllib And URLLIB2 are all related modules that accept URL requests, but provide different functionality. The two most notable differences are as follows:
urllib2
Can accept a Request
object to set the headers for a URL request, urllib
accepts only a URL. That's means, you cannot masquerade your User Agent string etc.
Urllib2 can accept an instance of the request class to set the URL request headers,urllib only the URL can be accepted. This means that you can not disguise your user agent string and so on.
urllib
Provides urlencode
the method which is used for the generation of GET query strings, urllib2
doesn ' t has such a function. This is one of the reasons, why are urllib
often used along with urllib2
.
Urllib provides the UrlEncode method for generating a Get query string, and Urllib2 does not. This is why Urllib often uses it with URLLIB2.
For other differences between urllib
urllib2
and refer to their documentations, the links is given in the References section .
Tip:if planning to do HTTP stuff only, check out httplib2, it's much better than Httplib or Urllib or URLLIB2.
Tip: If you're only doing HTTP-related, look at HTTPLIB2, which works better than several other modules.
Related information
Here's a similar question and answer: Urllib urllib2 What is the difference?
StackOverflow also have: should I use Urllib or urllib2?
Python Docs
- Urllib
- Urllib2
Differences between Python:urllib and URLLIB2 (ext.)