Start by importing webbrowser
:
1
|
import webbrowser
|
Next, simply call the webbrowser.open(url,new=0,) function
. The variable, url
, stands for the link you would like python to open using your default web browser (this will need to be of type ‘string’). The second variable, new
, represents the method the browser will attempt to use in order to open the url: 1) new=0 means the url is opened in the same browser window if possible, 2) new=1 means a new browser window is opened if possible, and 3) new=2 means a new browser page (“tab”) is opened if possible.
Here’s an example:
1
|
webbrowser. open (url = 'http://en.wikipedia.org/wiki/Main_Page' ,new = 2 )
|
Calling:
1
|
webbrowser.get()
|
with no arguments will display the default browser.
Lastly, if you seek to open a webpage using python through the command line, you can achieve this by:
1
|
python -m webbrowser -t "http://en.wikipedia.org/wiki/Main_Page"
|
The above ‘-t’ flag means to use a new tab, and can be switched for -n if you require a new browser window to be used instead. For more about the webbrowser module, see the documentation on their site.