Urllib module is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.
Urllib is a package that collects several modules for working with URLs, such as:
- urllib.request for opening and reading.
- urllib.parse for parsing URLs
- urllib.error for the exceptions raised
- urllib.robotparser for parsing robot.txt files
If urllib is not present in your environment, execute the below code to install it.
pip install urllib
Let’s see these in details.
This module helps to define functions and classes to open URLs (mostly HTTP). One of the most simple ways to open such URLs is :
We can see this in an example:
The source code of the URL i.e. Geeksforgeeks.
This module helps to define functions to manipulate URLs and their components parts, to build or break them. It usually focuses on splitting a URL into small components; or joining different URL components into URL string.
We can see this from the below code:
ParseResult(scheme='https', netloc='www.geeksforgeeks.org', path='/python-langtons-ant/', params='', query='', fragment='') https://www.geeksforgeeks.org/python-langtons-ant/
Note:- The different components of a URL are separated and joined again. Try using some other URL for better understanding.
Different other functions of urllib.parse are :
|urllib.parse.urlparse||Separates different components of URL|
|urllib.parse.urlunparse||Join different components of URL|
|urllib.parse.urlsplit||It is similar to urlparse() but doesn’t split the params|
|urllib.parse.urlunsplit||Combines the tuple element returned by urlsplit() to form URL|
|urllib.parse.urldeflag||If URL contains fragment, then it returns a URL removing the fragment.|
This module defines the classes for exception raised by urllib.request. Whenever there is an error in fetching a URL, this module helps in raising exceptions. The following are the exceptions raised :
- URLError – It is raised for the errors in URLs, or errors while fetching the URL due to connectivity, and has a ‘reason’ property that tells a user the reason of error.
- HTTPError – It is raised for the exotic HTTP errors, such as the authentication request errors. It is a subclass or URLError. Typical errors include ‘404’ (page not found), ‘403’ (request forbidden),
and ‘401’ (authentication required).
We can see this in following examples :
URL Error: urlopen error [Errno 11001] getaddrinfo failed
HTTP Error 403: Forbidden
This module contains a single class, RobotFileParser. This class answers question about whether or not a particular user can fetch a URL that published robot.txt files. Robots.txt is a text file webmasters create to instruct web robots how to crawl pages on their website. The robot.txt file tells the web scraper about what parts of the server should not be accessed.
For example :
None None True False
- Python | Parse a website with regex and urllib
- Import module in Python
- C API from Extension Module in Python | Set 1
- C Extension Module using Python
- OS Module in Python with Examples
- Python | Calendar Module
- OS Path module in Python
- Fraction module in Python
- C API from Extension Module in Python | Set 2
- Pygorithm module in Python
- Pylatex module in python
- Keyboard module in Python
- struct module in Python
- Python | Getting started with SymPy module
- Testing in Python using doctest module
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.