Python Webbrowser
In this post, you will learn about the webbrowser module of the Python programming language with some examples.
Python provides webbrowser module to interact with web browser applications. It has a high-level interface that includes functions to open urls in the browser. We can easily render static and dynamic web pages using this module.
In this article, you will learn how different tasks can be performed by python webbrowser module.
Python webbrowser open URL
The webbrowser.open() function opens the specified url on the browser. It has the following syntax-
webbrowser.open(url, new=0,autorise=true)
Here, new and autorise are optional parameters. If new = 0, the url is opened in the same browser. If the authorise is True, the window is raised if possible.
The below code opens the requested url in the default browser.
import webbrowser
webbrowser.open('https://www.etutorialspoint.com')
Python Open New Browser Window
The webbrowser.open_new() function opens the specified url on the new browser window. The below code opens the requested url in the default browser.
import webbrowser
webbrowser.open_new('https://www.etutorialspoint.com')
Python Open New Tab
The webbrowser.open_new_tab() function opens the specified url on the new tab. The below code opens the requested url in the default browser.
import webbrowser
webbrowser.open_new_tab('https://www.etutorialspoint.com')
Python Specify Browser Name
The webbrowser.get() function is used to specify the browser name. It returns a controller object for the specified browser name. In the given code, we have specified the windows default browser name.
import webbrowser
d = webbrowser.get('windows-default')
d.open('https://www.etutorialspoint.com')
Python Open Static Webpage
In the above examples, we have opened the specified url using the webbrowser module. This module also provides a feature to open html content in the browser. In the given example, we have opened a static html page in the browser.
import webbrowser
contents = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Python Webbrowser</title>
</head>
<body>
Hello, World!
</body>
</html>
'''
filename = 'webbrowser.html'
def main(contents, filename):
output = open(filename,"w")
output.write(contents)
output.close()
main(contents, filename)
webbrowser.open(filename)
Related Articles
Python convert list to numpy arrayPython Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python Tkinter Geometry Managers
Python Tkinter Scale Widget
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
Factorial Program in Python
Python snake game code with Pygame
Python JSON Tutorial - Create, Read, Parse JSON file
Python convert xml to dict
Python convert dict to xml