The main differences between the Python library urllib and urllib2 are analyzed

  • 2020-04-02 13:50:12
  • OfStack

As a Python novice, I had been ignorant of urllib and urllib2, thinking that 2 was an upgraded version of 1. Today saw a foreigner essay "(link: http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html) to understand the differences

You took a be intrigued by the existence of two separate URL modules in Python - urllib and urllib2. Even more intriguing. They are not alternatives for each other. So what is the difference between urllib and urllib2, and do we need them both.

You may be curious about the two independent -urllib2 and -urllib2 in Python. What's more interesting: they're not interchangeable. So what's the difference between these two modules, and do we need both of them?

Urllib and   Urllib2are both Python modules that do URL request related to stuff but offer different functionalities. Their two most significant differences are listed below:

Urllib and urllib2 are related modules that accept URL requests, but offer different capabilities. The two most striking differences are as follows:

The & # 8226; Urllib2 can accept a (link: http://docs.python.org/library/urllib2.html#urllib2.Request) object to set the headers for a URL request, urllib accepts only a URL. That means, you always masquerade your User Agent string etc.

Urllib2 can accept an instance of the Request class to set the headers for the URL Request, and urllib can only accept the URL. This means that you cannot disguise your User Agent string etc.

The & # 8226; Urllib provides the (link: http://docs.python.org/library/urllib.html#urllib.urlencode) method which is 2 for the generation of the GET query strings, Urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

Urllib provides the urlencode method to generate the GET query string, but urllib2 does not. This is why urllib is often used with urllib2.  

  For other differences between urllib and urllib2 refer to their documentations, the links are given in the References section.
Tip: if you are planning to do HTTP stuff only, check out httplib2, it is much better than httplib or urllib or urllib2.

Tip: if you only do HTTP related, see (link: http://code.google.com/p/httplib2/), several modules to use than others.

The relevant data

There is a similar (link: http://www.velocityreviews.com/forums/t326690-urllib-urllib2-what-is-the-difference.html) : urllib urllib2 what is the difference & # 63;

Stackoverflow also have: (link: http://stackoverflow.com/questions/2018026/should-i-use-urllib-or-urllib2)

Python Docs

(link: http://docs.python.org/library/urllib.html) (link: http://docs.python.org/library/urllib2.html)

Related articles: