Python

How to open URL in python

As technology advances, the ability to access and retrieve information from the internet is becoming increasingly essential. Python, a versatile programming language, offers several methods to open URLs programmatically.

In this tutorial, various approaches to open URLs in Python will be discussed.

What is URL Management?

The operations including opening, reading, writing, and altering URLs are all part of URL management. The web pages, photos, documents, and other resources may all be found using URLs. Programmatically handling URLs is necessary for operations like web scraping, downloading data, and communicating with web APIs.

How to Open a URL in Python?

URLs in Python can be opened via the following approaches:

Method 1: Open a URL Using the “webbrowser” Module

Python’s built-in “webbrowser” module allows users to open URLs using the default browser installed on their system. This module is particularly useful when quick and effortless URL opening is required, as it simplifies the process with just a single function call.

Make sure the web browser library is correctly installed on the system where Python is being used before. If the webbrowser module is not already installed, install it by entering the following command in the device’s command line:

pip install webbrowser

Example 1: Opening the URL in a New Tab

Let’s take a look at how we can open URLs using the “webbrowser” module in a new tab:

import webbrowser

url= "https://linuxhint.com/"

webbrowser.open_new_tab(url)

In the above code, import the “webbrowser module. After that, specify the URL and use the “webbrowser.open_new_tab()” function to open that URL in a new tab.

Output

Example 2: Opening the URL in the Specified Browser

Now, we can also specify the browser we want to open the URL in. Overview of the following code:

import webbrowser

url = "https://linuxhint.com/bash-subshells/"

webbrowser.get('Chrome').open(url)

In these code lines:

  • Firstly, import the “webbrowser” module.
  • Now, specify the URL and use the “webbrowser.get()” function to open that URL on the “Chrome” browser.
  • We will receive the type of response listed as an output if we attempt to launch the browser of our choice without registering it first.

Output

As seen, the stated error is generated.

The adjusted code after registering the browser is provided below that opens that URL:

import webbrowser

url = "https://linuxhint.com/"

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))

webbrowser.get('chrome').open(url)

In this code:

  • Firstly, import the “webbrowser” module. The Google Chrome executable’s path on the system is then placed in the “chrome_path” variable.
  • The “webbrowser.register()” method is then used to register the new browser “chrome” using the webbrowser module.
  • In this step, the supplied “chrome_path” path is linked to the “chrome” browser.
  • Lastly, the “open()” method opens a new tab redirecting to the “https://linuxhint.com/” site.

Output

Method 2: Open a URL Using Python Shell

There is yet another way to use Python’s built-in web browser to open the browser. With this approach, opening the browsers doesn’t require creating the entire script and interpreting it. A single command (provided below) could be used to launch the default browser and open a specific URL using the Python shell:

python -m webbrowser -t "https://linuxhint.com/"

Output

The corresponding URL is opened successfully.

Conclusion

As the need to access and retrieve information from the internet grows, knowing how to open URLs programmatically becomes crucial for developers. By utilizing Python modules like webbrowser, developers can open URLs, and specify and register the browsers of their choice. They can also open the URLs in one command via the Python shell.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.