Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convenient Web-browser controller in Python
To display web-based documents to users in Python, there is a module called webbrowser. It provides a high-level interface to handle web documents and control web browsers programmatically.
On UNIX-based systems, this module supports lynx, Netscape, Mosaic and other browsers. For Windows and macOS, it uses the standard browsers.
Importing the Module
To use this module, we need to import it first ?
import webbrowser
Main Methods
webbrowser.open(url, new=0, autoraise=True)
This method opens the specified URL using the default web browser. The new parameter controls how the URL opens ?
- new=0: Opens in the same browser window
- new=1: Opens in a new browser window
- new=2: Opens in a new browser tab
import webbrowser
# Open in same window
webbrowser.open('https://www.tutorialspoint.com')
# Open in new window
webbrowser.open('https://www.tutorialspoint.com', new=1)
# Open in new tab
webbrowser.open('https://www.tutorialspoint.com', new=2)
webbrowser.open_new(url)
This method opens the URL in a new browser window ?
import webbrowser
webbrowser.open_new('https://www.tutorialspoint.com')
webbrowser.get(using=None)
This method returns a controller for the specified browser type. If using is None, it returns controller for the default browser ?
import webbrowser
# Get default browser controller
browser_ctrl = webbrowser.get()
browser_ctrl.open('https://www.tutorialspoint.com')
# Get specific browser controller (Windows)
chrome_ctrl = webbrowser.get('chrome')
chrome_ctrl.open('https://www.tutorialspoint.com')
Predefined Browser Types
The webbrowser module supports various browser types that can be passed to the get() method ?
| Browser Type | Class Name | Description |
|---|---|---|
'mozilla' |
Mozilla('mozilla') | Mozilla Firefox browser |
'firefox' |
Mozilla('firefox') | Firefox browser |
'chrome' |
Chrome('chrome') | Google Chrome browser |
'safari' |
MacOSX('safari') | Safari browser (macOS) |
'opera' |
Opera() | Opera browser |
'windows-default' |
WindowsDefault | Default Windows browser |
Complete Example
Here's a practical example demonstrating browser control ?
import webbrowser
# Method 1: Using default browser
print("Opening in default browser...")
webbrowser.open('https://www.tutorialspoint.com')
# Method 2: Getting browser controller
try:
# Try to get Chrome browser
browser_ctrl = webbrowser.get('chrome')
print("Opening in Chrome...")
browser_ctrl.open_new_tab('https://www.tutorialspoint.com/python/index.htm')
except webbrowser.Error:
print("Chrome not found, using default browser")
webbrowser.open('https://www.tutorialspoint.com/python/index.htm')
# Method 3: Open multiple URLs
urls = [
'https://www.tutorialspoint.com',
'https://www.tutorialspoint.com/python',
'https://www.tutorialspoint.com/pandas'
]
for url in urls:
webbrowser.open_new_tab(url)
print(f"Opened: {url}")
Opening in default browser... Opening in Chrome... Opened: https://www.tutorialspoint.com Opened: https://www.tutorialspoint.com/python Opened: https://www.tutorialspoint.com/pandas
Exception Handling
The webbrowser.Error exception is raised when there's an error in the webbrowser interface ?
import webbrowser
try:
browser_ctrl = webbrowser.get('nonexistent-browser')
browser_ctrl.open('https://www.tutorialspoint.com')
except webbrowser.Error as e:
print(f"Browser error: {e}")
print("Using default browser instead")
webbrowser.open('https://www.tutorialspoint.com')
Conclusion
The webbrowser module provides a convenient way to control web browsers from Python scripts. Use webbrowser.open() for simple URL opening or get() method for specific browser control.
