Main differences between the Python library urllib and urllib2: urlliburllib2

Source: Internet
Author: User

Main differences between the Python library urllib and urllib2: urlliburllib2

As a Python cainiao, I had been ignorant of urllib and urllib2, and thought 2 was an upgraded version of 1. Today, I saw a piece of "Python: difference between urllib and urllib2" written by foreigners to understand the difference.

You might 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 is that they cannot be replaced by each other. So what is the difference between the two modules, and do we need them?

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

Both urllib and urllib2 accept URL request modules, but provide different functions. The two most significant differences are as follows:

• Urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL. That means, you cannot masquerade your User Agent string etc.

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

• Urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib.

Urllib provides the urlencode method to generate GET query strings, 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 perform HTTP-related tasks, check httplib2, which is better than other modules.

Related Materials

Here is a similar Q & A: urllib urllib2 what is the difference?

Stackoverflow also has: shocould I use urllib or urllib2?

Python Docs

  1. Urllib
  2. Urllib2

The difference between urllib and urllib2 in python is

You can use urllib2 as the extension of urllib. The obvious advantage is that urllib2.urlopen can accept the Request object as a parameter and thus control the header of the HTTP Request.
Urllib2 library should be used whenever possible during HTTP Request, but a series of quote and unquote functions such as urllib. urlretrieve function and urllib. quote are not added to urllib2. Therefore, urllib is also needed.

What is the difference between urllib and urllib2?

The latter provides more functions.
However, python3 has been merged into one.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.