What are the differences between the urllib, urllib2, urllib3 and requests module?

urllib, urllib2, and urllib3 are all Python standard library modules for handling URLs. They have similar functionality but have evolved over time to add new features and address bugs.

urllib is the original module for handling URLs in Python. It is part of the Python standard library in Python versions 1 and 2. It includes the urlopen function for opening URLs, the urlretrieve function for retrieving URLs to a local file, and other functions for handling URLs.

Watch a course Python - The Practical Guide

urllib2 was added in Python 2 as an updated version of urllib. It includes new features such as the ability to handle HTTP redirects and adding headers to requests.

urllib3 is an external library that is not part of the Python standard library, but provides similar functionality to urllib and urllib2. It is designed to be more efficient and secure than the built-in modules.

requests is an external library that is not part of the Python standard library, but is a popular third-party library for handling HTTP requests. It is designed to be more user-friendly and Pythonic than the built-in modules.

Here is an example of how to use the requests module to make a GET request to a website and retrieve the response:

import requests

response = requests.get('https://www.example.com')
print(response.text)