Python Urllib Module

13 Oct, 2021
Urllib package 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.
urllib.request
This module helps to define functions and classes to open URLs (mostly HTTP). One of the most simple ways to open such URLs is :urllib.request.urlopen(url)We can see this in an example:
import urllib.requestrequest_url = urllib.request.urlopen('https://www.geeksforgeeks.org/')print(request_url.read())
The source code of the URL i.e. Geeksforgeeks.

urllib.parse
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 strings.We can see this from the below code:
from urllib.parse import * parse_url = urlparse('https://www.geeksforgeeks.org / python-langtons-ant/')print(parse_url)print("\n")unparse_url = urlunparse(parse_url)print(unparse_url)
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.errorThis 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  import urllib.requestimport urllib.parse  # trying to read the URL but with no internet connectivitytry:    x = urllib.request.urlopen('https://www.google.com')    print(x.read())  # Catching the exception generated     except Exception as e :    print(str(e))
URL Error: urlopen error [Errno 11001] getaddrinfo failed

# HTTP Error  import urllib.requestimport urllib.parse  # trying to read the URLtry:    x = urllib.request.urlopen('https://www.google.com / search?q = test')    print(x.read())  # Catching the exception generated    except Exception as e :    print(str(e))
HTTP Error 403: Forbidden

urllib.robotparserThis 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 :
# importing robot parser classimport urllib.robotparser as rb  bot = rb.RobotFileParser()  # checks where the website's robot.txt file residex = bot.set_url('https://www.geeksforgeeks.org / robot.txt')print(x)  # reads the filesy = bot.read()print(y)  # we can crawl the main sitez = bot.can_fetch('*', 'https://www.geeksforgeeks.org/')print(z)  # but can not crawl the disallowed urlw = bot.can_fetch('*', 'https://www.geeksforgeeks.org / wp-admin/')print(w)
None
None
True
False

shivakumar27
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON