21.1.WebBrowser-convenient Web-browser Controller
Source Code: lib/webbrowser.py translation: Z.f.
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 would do the right thing.
Under Unix, graphical browsers is preferred under X11, but Text-mode browsers would be used if graphical browsers is not Available or an X11 display isn ' t available. If Text-mode browsers is used, the calling process would block until the user exits the browser.
IF the environment variable BROWSER exists, it is interpreted as the os.pathsep-separated List of Browse RS to try ahead of the platform defaults. 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's simply interpreted as the name of the browser to launch. [1]
For Non-unix platforms, or when a remote browser was available on Unix, the controlling process would not wait for the user To-finish with the browser, but allow the remote browser-maintain its own windows on the display. If remote browsers is not available on Unix, the controlling process would launch a new browser and wait.
The script WebBrowser can 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. Usage Example:
The following exception is defined:
-
Exception WebBrowser. Error
-
Exception raised when a browser control error occurs.
The following functions is defined:
- WebBrowser. Open (
url ,
new=0 ,
autoraise=true )
The
-
Display URL using the default browser. If new is 0, the URL was 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 and the window is raised if possible (note that under many window managers This would 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 a Ssociated program. However, this is neither supported nor portable.
- WebBrowser. open_new (
url )
-
Open URL in a new window of the default browser, if possible, otherwise, open URL in the only browser WI Ndow.
- WebBrowser. Open_new_tab (
url )
-
Open URL in a new page ("tab") of the default browser, if possible, otherwise equivalent to open_new () .
- WebBrowser. Get (
using=none )
-
Return A controller object for the browser type using . If using is none , return a controller for a default browser appropriate to the caller ' s ENVI Ronment.
- WebBrowser. Register (
name ,
constructor ,
instance=none )
-
Register The browser type name . Once A browser type is registered, the none ."
This entry point is only useful if you plan to either set the browser variable or call
A number of browser types is predefined. This table gives the type names the is passed to the get () function and the corresponding instantiations for The controller classes, all defined in this module.
Type Name |
Class Name |
Notes |
' Mozilla ' |
Mozilla (' Mozilla ') |
|
' Firefox ' |
Mozilla (' Mozilla ') |
|
' Netscape ' |
Mozilla (' Netscape ') |
|
' Galeon ' |
Galeon (' Galeon ') |
|
' Epiphany ' |
Galeon (' Epiphany ') |
|
' Skipstone ' |
Backgroundbrowser (' Skipstone ') |
|
' Kfmclient ' |
Konqueror () |
(1) |
' Konqueror ' |
Konqueror () |
(1) |
' KFM ' |
Konqueror () |
(1) |
' Mosaic ' |
Backgroundbrowser (' mosaic ') |
|
' Opera ' |
Opera () |
|
' Grail ' |
Grail () |
|
' Links ' |
Genericbrowser (' links ') |
|
' ELinks ' |
ELinks (' ELinks ') |
|
' Lynx ' |
Genericbrowser (' Lynx ') |
|
' W3m ' |
Genericbrowser (' w3m ') |
|
' Windows-default ' |
Windowsdefault |
(2) |
' MacOSX ' |
MacOSX (' Default ') |
(3) |
' Safari ' |
MacOSX (' Safari ') |
(3) |
' Google-chrome ' |
Chrome (' Google-chrome ') |
|
' Chrome ' |
Chrome (' Chrome ') |
|
' Chromium ' |
Chromium (' Chromium ') |
|
' Chromium-browser ' |
Chromium (' Chromium-browser ') |
|
Notes:
- "Konqueror" is the file manager for the KDE desktop environment for Unix, and only makes sense to use if KDE is running. Some the reliably detecting KDE would is nice; The kdedir variable is not sufficient. Note also that the name "KFM" was used even when using the konqueror command with KDE 2-the implementation Selec TS the best strategy for running Konqueror.
- Only on Windows platforms.
- Only on Mac OS X platform.
New in version 3.3:support for Chrome/chromium have been added.
Here is some simple examples:
url = ' http://docs.python.org/' # Open URL in a new tab, if a browser window is already open. Webbrowser.open_new_tab ( Url
21.1.1. Browser Controller Objects
Browser controllers provide these methods which parallel three of the Module-level convenience functions:
- Controller. Open (
url,
new=0,
autoraise=true )
-
Display URL using the browser handled by this controller. 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.
- Controller. open_new (
url )
-
Open URL in a new window of the browser handled by this controller, if possible, otherwise, open URL in The only browser window. Alias open_new ().
- Controller. Open_new_tab (
url )
-
Open URL in a new page ("tab") of the browser handled by this controller, if possible, otherwise equivalent to open_new ().
Footnotes
[1] |
Executables named here without a full path would be is searched in the directories given in the path environment vari Able. |
Python Standard library 3.4.3-webbrowser