Javatpoint Logo
Javatpoint Logo

How to open URL in Python

Python is a very strong and advanced programming language, and we can perform various tasks and functions using Python. One of such tasks that we can easily do with the help of Python is opening a url with a Python program. In this tutorial, we are going to discuss the methods or ways which we can use to open a url in Python.

Opening url in Python

We can use a Python program to open a url using the Python script, and for this, we can use a different set of libraries. We have different methods in which we will use different libraries and their functions to open a url given in the program.

We are going to use the following methods in this section to open a given url using a Python program:

  1. Using Urllib library function
  2. Using webbrowser library function
  3. Using selenium library function

In all three libraries, as we have mentioned above, the first two libraries are generally coming pre-installed with the latest Python versions. We are going to discuss all three methods and we will use a Python program in each to better understand their implementation.

Method 1: Using urllib library function

Urllib is an inbuilt Python module that we can use to work on urls and open url using a Python program. In the urllib module, various classes and functions are defined, which help us to perform various url actions using a Python program.

We will use the urlopen() method by importing urllib.request library in the program, and then we give url inside this function so that it will open in the browser of our device. To better understand the implementation of this method of using urlopen(), we will use it in an example Python program and open a link through it.

Example 1: Look at the following Python program where we using urlopen() function:

Output:

How to open URL in Python

Explanation: We have opened the url of 'Python tutorial in JavaTpoint' in our browser by giving the url link inside the urlopen() function in the program.

Method 2: Using webbrowser library function:

Now, we will use the webbrowser library, which is a library in Python to work with the web-based content. An environment is created for the user by using the webbrowser module that enables the user to display various web-based contents in the Python application itself.

Before we start working with the webbrowser library, we should make sure that it is properly installed in our system where we are running Python. And, if the webbrowser library is not present in the system, then we can install the same by using the following command in the command prompt of our device.

Now, we will start working with the webbrowser library and open url with the webbrowser library; we will use the open() function of it in the program. To better understand the implementation of this method of using the webbrowser library for opening url in Python, we will use it in an example Python program and open a link through it.

Example 2: Look at the following Python program:

Output:

How to open URL in Python

Explanation: We have opened the url in the chrome browser of our system by using the open_new_tab() function of the webbrowser module and providing url link in it.

Method 3: Using selenium library function:

Selenium library is a powerful tool provided of Python, and we can use it for controlling the URL links and web browser of our system through a Python program. We can also open url links in our Python program using the selenium library functions. The binding of selenium library functions in Python provides us with some very convenient APIs to access the WebDrivers of selenium modules like Chrome, Firefox, Microsoft Edge etc.

Installation of selenium library

Before we start working with the selenium library, we should make sure that the selenium library is properly installed in our system where we are running Python. To install the selenium library, we have to use the following command with the pip installer in the command prompt terminal:


How to open URL in Python

We have successfully installed the selenium library in our system, as we can see above. Currently, the Python versions that support the Selenium library are Python 2.7, Python 3.5 and all the higher versions.

Installation of web drivers:

To use the selenium library functions with URLs, we also have to install the web drivers provided by selenium for the respective browser.

WebDriver is a package provided by the selenium to interact with the web browser present in our system. The WebDrivers interacts with the remote web server or the web browser of the system using a wire protocol common for all the web browsers. Here, we are providing links to download the web drivers of some famous browsers and using these links; you can download web drivers for the web browser present in the system.

Web driver for Safari:

Web driver for Firefox:

Web driver for Chrome:

Now, we will start working with the selenium library and open url by using the selenium library functions in the program. To better understand the implementation of this method of using selenium library for opening url in Python, we will use it in an example Python program and open a link through it.

Example 3: Look at the following Python program:

Output:

How to open URL in Python
How to open URL in Python

Explanation: After importing the webdrivers from the selenium library, we imported the keys from the webdrivers in the program. We provided the path for the chrome webdriver we downloaded using the above-given links. Then, with the help of the get() function with the driver, we have opened the url in the browser by giving a link in the get() method.

By using any of the three methods we have mentioned in this tutorial, we can easily open the url inside the web browser of our system with the help of a Python program.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA