As a Python rookie, has been ignorant of urllib and urllib2, thought 2 is 1 of the upgraded version. Today, I saw the foreigner wrote an article "Python:difference between Urllib and Urllib2" to understand the difference between
You might is intrigued by the existence of two separate URL modules in Python-urllib and URLLIB2. Even more intriguing:they are is not alternatives to each other. So what are the difference between urllib and urllib2, and do we need them?
You may be curious about the two independent-urllib2 and-urllib2 in Python. What's even more interesting is that they don't have to be substituted for each other. So what's the difference between these two modules, and do we both need them?
Urllib and Urllib2are both Python modules that do URL request related stuff the offer but different. Their two most significant differences are:
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 a request class to set the URL request Headers,urllib can only accept URLs. This means that you can not disguise your user agent string, and so on.
Urllib provides the UrlEncode method which was used for the generation of Get query strings, Urllib2 doesn ' t have such a f Unction. This is one of the reasons why urllib are often used with along.
Urllib provides a UrlEncode method for the generation of a Get query string, while URLLIB2 does not. This is why urllib often used with URLLIB2.
For other differences between Urllib and Urllib2 refer to their documentations, the links are given in the References sect Ion.
Tip:if you are planning to do HTTP stuff only, check out httplib2, it are much better than httplib or Urllib or URLLIB2.
Tip: If you only do HTTP-related, look at the httplib2, better than a few other modules.
Related information
Here's a similar question and answer: Urllib urllib2 What is the difference?
StackOverflow also: Should I use Urllib or urllib2?
Python Docs