Python is an interpreted language; it has different libraries to perform various functions. A Uniform Resource Locator (URL) is actually a web address to open a specific site. Sometimes while working in python we need to fetch data from a website, for this we have to open the url of a specific website. So, to open a URL in python we need to import the specified module and perform some steps to open that URL. In this article we will discuss how to open a URL in Python using “urllib.request” and “webbrowser” modules on Ubuntu (Linux OS) through a defined procedure.

Requirement:

Any installed version of python (python3 is preinstalled on Ubuntu latest version)

Follow the any of the procedure explained below to open url in python:

How to Create Python file

Generate a file with “python_file.py” (python file) name by using “nano command” as mentioned below to write python code in it

You can change the name of the file according to your choice.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-8.jpg" data-lazy- height="216" src="data:image/svg xml,” width=”927″>

How to Open URL using “urllib.request” Module

The “urllib.request” is one of the modules of python that allows opening urls in python.

Step1: Importing “urllib.request” library

To open URL in python you firstly you need to import the “urllib.request”, insert below mentioned import code line before starting your code in the newly created “python_file.py” file:

Step2: Opening URL using urllib.request function

To open the URL of specific website using urllib.request, use the below mentioned syntax:

urllib.request.urlopen(‘website url’)

website URL: Insert the URL of the website which you want to fetch.

To open URL “ https://www.google.com/ “ , write the below mentioned code in your python file:

import urllib.request

get_url= urllib.request.urlopen(‘https://www.google.com/’)

print(“Response Status: “ str(get_url.getcode()) )

HTTP has defined response status codes; “get_url.getcode” is used to get that code. The digit “200” means your connection is successful, if it is “404” then that means the url is not recognized. Visit this source to learn about other status codes.

The “get_url” It is the variable that gets the data from the specified url and “print” is used to print the output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-8.jpg" data-lazy- height="680" src="data:image/svg xml,” width=”926″>

Press “Ctrl s” to save the file and “Ctrl x” to exit the file:

To check the working of code, run the below mentioned command to execute file “python_file.py”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-8.jpg" data-lazy- height="202" src="data:image/svg xml,” width=”929″>

Above output shows that the connection is successful.

Let’s check another example; we can also retrieve HTML code from the URL of any website. Run the below mentioned code to open url https://www.youtube.com/ and print its html code:

import urllib.request

get_url= urllib.request.urlopen(‘https://www.youtube.com/’)

print(“Response Status: “ str(get_url.getcode()))

print(get_url.read())

The “get_url.getcode()” is used to get http Response Status Code and “get_url.read()” is used to retrieve the html file of a website.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-7.jpg" data-lazy- height="679" src="data:image/svg xml,” width=”929″>

Run the below mentioned command to execute file “python_file.py”, to get the desired output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/5-7.jpg" data-lazy- height="835" src="data:image/svg xml,” width=”932″>

How to open URL using “webbrowser” Module

“webbrowser” is one of the modules of python which is also used to open URLs or websites in python but it directs the link to the browser.

To open URL using “webbrowser” module, follow the steps mentioned below:

Step1: Importing “webbrowser” library

To open URL, firstly you need to import the “webbrowser” library in the “python_file.py” by below mentioned code line:

Step2: Opening URL using webbrowser module

To open the URL of specific website on browser using “webbrowser”, use the below mentioned syntax:

webbrowser.open(“website_url”)

Insert your URL in place of “website_url” in above mentioned syntax.

To open the URL “https://linuxhint.com/” using “webbrowser” module, write the below mentioned code in “python_file.py” file:

import webbrowser

get_url= webbrowser.open(‘https://linuxhint.com/’)

The “get_url.getcode()” is used to get http Response Status Code , 200 means you have successfully opened the url.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/6-7.jpg" data-lazy- height="827" src="data:image/svg xml,” width=”926″>

To open the URL browser, execute the code written in “python_file.py” by below mentioned command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/7-3.jpg" data-lazy- height="888" src="data:image/svg xml,” width=”1849″>

Conclusion:

To get data from a website while programming, we need to open the URL. In this Article, I have discussed the methods to open URLs in python on Ubuntu (Linux System). Two ways are being discussed with examples, one is by importing the “urllib.request” module and other is by importing the “webbrowser” module in python. If you are a python programmer then after reading this article you will be able to open a URL in python with ease.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/7c99efcc024949e17b176845c757687e?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.