webbrowser Module:
Import webbrowser python: It is a Python module that displays web-based documents or web pages for the URL entered in the browser.
In the scenario that many browsers are present on the system, the module provides a registry of available browsers. The BROWSER environment variable can also be used to control it.
Let us look at several types of webbrowser functions and some examples based on them.
webbrowser Module in Python
1)Opening the URL in the default web Browser:
Approach:
- Import webbrowser module using the import keyword
- Open the link in the default web browser using webbrowser module by passing some random URL to the open() function
- The Exit of the Program.
Below is the implementation:
# Import webbrowser module using the import keyword import webbrowser # Open the link in the default webbrowser using webbrowser module by passing # some random url to the open() function webbrowser.open('https://python-programs.com/')
Output:
True
2)Opening the URL in the Browser New Window:
Approach:
- Import webbrowser module using the import keyword
- Open the link in the browser new window using webbrowser module by passing some random URL to the open_new() function.
- The Exit of the Program.
Below is the implementation:
# Import webbrowser module using the import keyword import webbrowser # Open the link in the browser new window using webbrowser module by passing # some random url to the open_new() function webbrowser.open_new('https://python-programs.com/')
Output:
True
3)Opening the URL in the New Browser Tab:
Approach:
- Import webbrowser module using the import keyword
- Open the link in the new browser tab using webbrowser module by passing some random URL to the open_new_tab() function
- The Exit of the Program.
Below is the implementation:
# Import webbrowser module using the import keyword import webbrowser # Open the link in the new browser tab using webbrowser module by passing # some random url to the open_new_tab() function webbrowser.open_new_tab('https://python-programs.com/')
Output:
True
4)Opening the URL in the Given Browser:
Approach:
- Get the particular web browser using the webbrowser module by passing some random browser name to the get() function.
- Open the link in the given particular browser using the given webbrowser by passing some random URL to the open() function
- Here it opens the URL in the firefox browser.
- Open the link in the given particular browser new tab using the given webbrowser by passing some random URL to the open_new_tab() function.
- The Exit of the Program.
Below is the implementation:
# Get the particular web browser using the webbrowser module by passing # some random browser name to the get() function firefox_browser = webbrowser.get('firefox') # Open the link in the given particular browser using the given webbrowser by passing # some random url to the open() function # Here it opens the URL in the firefox browser. firefox_browser.open('https://python-programs.com/') # Open the link in the given particular browser new tab using the given webbrowser by passing # some random url to the open_new_tab() function firefox_browser.open_new_tab('https://python-programs.com/')
Output:
True True