Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Open a URL in Your Browser From a Python Script?

#1
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 webbrowser.open('https://google.com') to open Google. Don’t forget to run import webbrowser first. But you don’t have to install the module because it’s already in Python’s standard library.


Example


Here’s an example Python script that opens the URL 'https://finxter.com':

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 webbrowser.open() function:


About the Webbrowser Module


The webbrowser 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:

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 webbrowser.open() function.

webbrowser.open(url, new=0, autoraise=True)

The new argument allows you to control the browser window:

  • If you set new=0 (default), you open the URL in the same browser window.
  • If you set new=1, you open a new browser window.
  • If you set new=2, you open a new browser tab.

The autoraise 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 webbrowser.open_new_tab() and pass your URL string as a single argument.

import webbrowser
my_url = 'https://finxter.com'
webbrowser.open_new_tab(my_url)

Select the Webbrowser


You can also return a controller object for a given browser by calling webbrowser.get() and passing the browser type into it. Now, you can call the open() or open_new_tab() methods on this controller object to open the URL in your desired web browser.

import webbrowser
webbrowser.get("chrome").open("https://finxter.com")

Here are the supported browser types:


Type Name Class Name
'mozilla' Mozilla('mozilla')
'firefox' Mozilla('mozilla')
'netscape' Mozilla('netscape')
'galeon' Galeon('galeon')
'epiphany' Galeon('epiphany')
'skipstone' BackgroundBrowser('skipstone')
'kfmclient' Konqueror()
'konqueror' Konqueror()
'kfm' Konqueror()
'mosaic' BackgroundBrowser('mosaic')
'opera' Opera()
'grail' Grail()
'links' GenericBrowser('links')
'elinks' Elinks('elinks')
'lynx' GenericBrowser('lynx')
'w3m' GenericBrowser('w3m')
'windows-default' WindowsDefault
'macosx' MacOSXOSAScript('default')
'safari' MacOSXOSAScript('safari')
'google-chrome' Chrome('google-chrome')
'chrome' Chrome('chrome')
'chromium' Chromium('chromium')
'chromium-browser' Chromium('chromium-browser')

Thanks for Reading ❤


To keep learning, feel free to check out our email academy and download our free Python cheat sheets. ?



https://www.sickgaming.net/blog/2022/10/...on-script/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Check Python Version from Command Line and in Script xSicKxBot 0 1,907 08-24-2023, 01:34 PM
Last Post: xSicKxBot
  [Tut] Top 7 Ways to Use Auto-GPT Tools in Your Browser xSicKxBot 0 1,285 08-14-2023, 12:38 PM
Last Post: xSicKxBot
  [Tut] Top 7 Ways to Use Auto-GPT Tools in Your Browser xSicKxBot 0 2,289 05-29-2023, 01:09 PM
Last Post: xSicKxBot
  [Tut] Python to .exe – How to Make a Python Script Executable? xSicKxBot 0 1,260 03-13-2023, 01:31 AM
Last Post: xSicKxBot
  [Tut] EzpzShell: An Easy-Peasy Python Script That Simplifies Revshell Creation xSicKxBot 0 1,339 02-11-2023, 10:30 AM
Last Post: xSicKxBot
  [Tut] Bitcoin – Trading Moving Averages or HODL? A Python Script Uncovers the Answer! xSicKxBot 0 1,350 01-18-2023, 03:08 AM
Last Post: xSicKxBot
  [Tut] I Used This Python Script to Check a Website Every X Seconds for a Given Word xSicKxBot 0 1,234 01-11-2023, 11:00 PM
Last Post: xSicKxBot
  [Tut] A Python Script to Uncover Creative Research Gaps Through Recombination xSicKxBot 0 1,218 01-04-2023, 07:58 AM
Last Post: xSicKxBot
  [Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop xSicKxBot 0 1,106 12-18-2022, 11:52 AM
Last Post: xSicKxBot
  [Tut] Bash Port Scanning (SSH) as a Python Script [TryHackMe] xSicKxBot 0 1,283 11-24-2022, 10:10 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016