Python

How to open URL in python

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

$ nano python_file.py

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

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:

import urllib.request

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.

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”.

$ python3 python_file.py

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.

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

$ python3 python_file.py

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:

import webbrowser

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.

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

$ python3 python_file.py

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

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.