11-02-2022, 04:51 PM
How to Open a URL in Your Browser From a Python Script?
5/5 – (1 vote)
To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call to open Google. Don’t forget to run first. But you don’t have to install the module because it’s already in Python’s standard library.
![[Image: open_url.gif]](https://blog.finxter.com/wp-content/uploads/2022/10/open_url.gif)
Example
Here’s an example Python script that opens the URL:
A new browser tab with your default browser (Chrome, Edge, Safari, Brave — whatever you set up as standard browser in your OS settings) opens, initialized with the URL provided as a string argument of the function:
![[Image: image-295-1024x595.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-295-1024x595.png)
About the Webbrowser Module
The module is already part of the Python Standard Library, so you can import it without needing to install it first.
You can also run the module from your command line or terminal by using the following command:
Good to know if you ever want to open a URL from your operating system command line or terminal (Windows, macOS, Linux, Ubuntu) because the fact that you use Python makes it portable and operating system independent!
Webbrowser open()
You can specify additional arguments to get more control over which tab is opened by means of the new argument of the function.
The argument allows you to control the browser window:
The argument allows you to raise the window (default behavior).
Webbrowser Open in New Tab
A short way of opening a given URL in a new tab from your Python script is to call and pass your URL string as a single argument.
Select the Webbrowser
You can also return a controller object for a given browser by calling and passing the browser type into it. Now, you can call the or methods on this controller object to open the URL in your desired web browser.
Here are the supported browser types:
Type Name |
Class Name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks for Reading![[Image: 2764.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png)
To keep learning, feel free to check out our email academy and download our free Python cheat sheets.
5/5 – (1 vote)
To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call
Code:
webbrowser.open('https://google.com')Code:
import webbrowser![[Image: open_url.gif]](https://blog.finxter.com/wp-content/uploads/2022/10/open_url.gif)
Example
Here’s an example Python script that opens the URL
Code:
'https://finxter.com'Code:
import webbrowser
webbrowser.open('https://finxter.com/')A new browser tab with your default browser (Chrome, Edge, Safari, Brave — whatever you set up as standard browser in your OS settings) opens, initialized with the URL provided as a string argument of the
Code:
webbrowser.open()![[Image: image-295-1024x595.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-295-1024x595.png)
About the Webbrowser Module
The
Code:
webbrowserYou can also run the module from your command line or terminal by using the following command:
Code:
python -m webbrowser -t "https://finxter.com"Good to know if you ever want to open a URL from your operating system command line or terminal (Windows, macOS, Linux, Ubuntu) because the fact that you use Python makes it portable and operating system independent!
Webbrowser open()
You can specify additional arguments to get more control over which tab is opened by means of the new argument of the
Code:
webbrowser.open()Code:
webbrowser.open(url, new=0, autoraise=True)The
Code:
new- If you set (default), you open the URL in the same browser window.Code:
new=0
- If you set , you open a new browser window.Code:
new=1
- If you set , you open a new browser tab.Code:
new=2
The
Code:
autoraiseWebbrowser Open in New Tab
A short way of opening a given URL in a new tab from your Python script is to call
Code:
webbrowser.open_new_tab()Code:
import webbrowser
my_url = 'https://finxter.com'
webbrowser.open_new_tab(my_url)You can also return a controller object for a given browser by calling
Code:
webbrowser.get()Code:
open()Code:
open_new_tab()Code:
import webbrowser
webbrowser.get("chrome").open("https://finxter.com")Here are the supported browser types:
Type Name |
Class Name
Code:
'mozilla'Code:
Mozilla('mozilla')Code:
'firefox'Code:
Mozilla('mozilla')Code:
'netscape'Code:
Mozilla('netscape')Code:
'galeon'Code:
Galeon('galeon')Code:
'epiphany'Code:
Galeon('epiphany')Code:
'skipstone'Code:
BackgroundBrowser('skipstone')Code:
'kfmclient'Code:
Konqueror()Code:
'konqueror'Code:
Konqueror()Code:
'kfm'Code:
Konqueror()Code:
'mosaic'Code:
BackgroundBrowser('mosaic')Code:
'opera'Code:
Opera()Code:
'grail'Code:
Grail()Code:
'links'Code:
GenericBrowser('links')Code:
'elinks'Code:
Elinks('elinks')Code:
'lynx'Code:
GenericBrowser('lynx')Code:
'w3m'Code:
GenericBrowser('w3m')Code:
'windows-default'Code:
WindowsDefaultCode:
'macosx'Code:
MacOSXOSAScript('default')Code:
'safari'Code:
MacOSXOSAScript('safari')Code:
'google-chrome'Code:
Chrome('google-chrome')Code:
'chrome'Code:
Chrome('chrome')Code:
'chromium'Code:
Chromium('chromium')Code:
'chromium-browser'Code:
Chromium('chromium-browser')Thanks for Reading
![[Image: 2764.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png)
To keep learning, feel free to check out our email academy and download our free Python cheat sheets.

