How to open Webpage using Python

From Python, a call can be sent to some web browser for opening a Url. For doing this you can use webbrowser python module. Let’s first see what’s Webbrowser Python Module and What functions does it contain?

Web Browser Python Module Functions

Below is a table containing Web Browser module’s functions with descriptions for each one.

Web Browser Module FunctionDescription
webbrowser.open(url, new, autoraise=True)Open url using default browser
webbrowser.open_new(url)Open url in a new window of default browser
webbrowser.open_new_tab(url)Open url in new tab of default browser

Opening Webpage using Python’s webbrowser.open() Function

Syntax of Function : webbrowser.open(parameters)
Parameters : url, new, autoraise

Value of parameter new will decide whether to open new window or browser tab for opening the url.
• new = 0 means url will be opened in same browser window
• new = 1 means url will be opened in new browser window
• new = 2 means url will be opened in new Tab, if browser is already opened

Value of parameter autoraise if sets to True will make Python to raise up new window for url.

import webbrowser

webbrowser.open('https://computersciencehub.io', new=0, autoraise=True)
import webbrowser

webbrowser.open('https://computersciencehub.io', new=1, autoraise=True)
import webbrowser

webbrowser.open('https://computersciencehub.io', new=2, autoraise=True)

Opening Webpage using Python’s webbrowser.open_new() Function

Syntax of Function : webbrowser.open_new(parameter)
Parameters : url

This function webbrowser.open_new(url) opens a new window of default browser and opens url in it.

import webbrowser

webbrowser.open_new('https://computersciencehub.io')

Opening Webpage using Python’s webbrowser.open_new_tab() Function

Syntax of Function : webbrowser.open_new_tab(parameter)
Parameter : url

This function webbrowser.open_new_tab(parameter) opens a new tab of default browser and then opens url in it.

import webbrowser

webbrowser.open_new_tab('https://computersciencehub.io')

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts