Python webbrowser Library

Hello coders, in this tutorial we will study about webbrowser module or library in Python with code example. Before we proceed we will discuss the webbrowser library then we will discuss some of its functions.

What is the webbrowser Library?

It is a Python module used for displaying web-based documents or web pages for the URL provided in the browser.

Now we will see different types of webbrowser functions and see some example based on it.

CODE 1:-OPEN IN A BROWSER

It will open the URL in the default browser.

import webbrowser 
webbrowser.open('http://www.python.org') 

Output:-

True

CODE 2:-OPEN THE PROVIDED URL IN THE BROWSER NEW WINDOW

webbrowser.open_new('http://www.python.org') 

Output:-

True

CODE 3:-OPEN THE URL IN THE NEW BROWSER TAB

This will open the URL in the browser’s new tab if possible.

webbrowser.open_new_tab('http://www.python.org') 

Output:-

True

CODE 4:-OPEN THE URL IN THE SPECIFIC BROWSER

In the webbrowser module, there is a function of name get that will provide the user with a specific browser. And then we will launch the URL in the normal way as like code 1.

c = webbrowser.get('firefox') 
c.open('http://www.python.org') #line 2
c.open_new_tab('http://docs.python.org')  #line3

Output:-

True
True

Line 2:- This will open the URL in the specific browser.

Line 3:- This will open the URL in the specific browser new window.

I hope, you have understood the usage of the webbrowser module of Python and learn how simple and easy to use it.

Leave a Reply

Your email address will not be published.