20.1. webbrowser — Convenient Web-browser controller¶
The webbrowser module provides a high-level interface to allow displaying
Web-based documents to users. Under most circumstances, simply calling the
open() function from this module will do the right thing.
Under Unix, graphical browsers are preferred under X11, but text-mode browsers will be used if graphical browsers are not available or an X11 display isn’t available. If text-mode browsers are used, the calling process will block until the user exits the browser.
If the environment variable BROWSER exists, it is interpreted to
override the platform default list of browsers, as a os.pathsep-separated
list of browsers to try in order. When the value of a list part contains the
string %s, then it is interpreted as a literal browser command line to be
used with the argument URL substituted for %s; if the part does not contain
%s, it is simply interpreted as the name of the browser to launch. [1]
For non-Unix platforms, or when a remote browser is available on Unix, the controlling process will not wait for the user to finish with the browser, but allow the remote browser to maintain its own windows on the display. If remote browsers are not available on Unix, the controlling process will launch a new browser and wait.
The script webbrowser can be used as a command-line interface for the
module. It accepts an URL as the argument. It accepts the following optional
parameters: -n opens the URL in a new browser window, if possible;
-t opens the URL in a new browser page (“tab”). The options are,
naturally, mutually exclusive.
The following exception is defined:
-
exception
webbrowser.Error¶ Exception raised when a browser control error occurs.
The following functions are defined:
-
webbrowser.open(url[, new=0[, autoraise=True]])¶ Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is
True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
Changed in version 2.5:
Changed in version 2.5: new can now be 2.
-
webbrowser.open_new(url)¶ Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
-
webbrowser.open_new_tab(url)¶ Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to
open_new().New in version 2.5:
New in version 2.5.