Latest Code Tutorials

Python urllib.request.urlopen() Function with Example


The urllib is an inbuilt Python module that handles the URL efficiently. The urllib library is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and can fetch URLs using a variety of different protocols.

The Urllib is a package that collects several modules for working with URLs, such as:

  1. The urllib.request module is used for opening and reading.
  2. The urllib.parse module is used for parsing URLs.
  3. The urllib.error module is used for the exceptions raised.
  4. The urllib.robotparser module is used for parsing robot.txt files.

Python urllib.request Module

The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies, and more.

Python urllib.request.urlopen()

The urllib.request.urlopen() is an inbuilt Python method that opens the URL url, either a string or a Request object. The data must be an object specifying additional data to be sent to the server, or None if no such data is needed.


urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, 
                       capath=None, cadefault=False, context=None)


The urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS, and FTP connections.

If the context is specified, it must be an SSL.SSLContext instance describing the various SSL options. See HTTPSConnection for more details.

The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be found in SSL.SSLContext.load_verify_locations().

The cadefault parameter is ignored.


import urllib.request

req_url = urllib.request.urlopen("")

You can also use the with statement with the above code.

import urllib.request

with urllib.request.urlopen('') as response:
    html =

That is it for Python urllib.request.urlopen() function with example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.