webbrowser.rst 9.78 KB
Newer Older
1 2 3 4 5
:mod:`webbrowser` --- Convenient Web-browser controller
=======================================================

.. module:: webbrowser
   :synopsis: Easy-to-use controller for Web browsers.
6

7 8 9
.. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>

Raymond Hettinger's avatar
Raymond Hettinger committed
10 11 12
**Source code:** :source:`Lib/webbrowser.py`

--------------
13 14 15

The :mod:`webbrowser` module provides a high-level interface to allow displaying
Web-based documents to users. Under most circumstances, simply calling the
16
:func:`.open` function from this module will do the right thing.
17 18 19 20 21 22

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.

23
If the environment variable :envvar:`BROWSER` exists, it is interpreted as the
24
:data:`os.pathsep`-separated list of browsers to try ahead of the platform
25 26 27 28
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 is simply
interpreted as the name of the browser to launch. [1]_
29 30 31 32 33 34 35 36

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 :program:`webbrowser` can be used as a command-line interface for the
37
module. It accepts a URL as the argument. It accepts the following optional
38 39
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,
40 41 42
naturally, mutually exclusive.  Usage example::

   python -m webbrowser -t "http://www.python.org"
43 44 45 46 47 48 49 50 51 52 53

The following exception is defined:


.. exception:: Error

   Exception raised when a browser control error occurs.

The following functions are defined:


54
.. function:: open(url, new=0, autoraise=True)
55

56 57 58 59 60 61
   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).
62

Benjamin Peterson's avatar
Benjamin Peterson committed
63 64 65 66
   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.

67 68 69 70 71 72 73 74 75 76 77 78

.. function:: open_new(url)

   Open *url* in a new window of the default browser, if possible, otherwise, open
   *url* in the only browser window.

.. function:: open_new_tab(url)

   Open *url* in a new page ("tab") of the default browser, if possible, otherwise
   equivalent to :func:`open_new`.


79
.. function:: get(using=None)
80

81 82 83
   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 environment.
84 85


86
.. function:: register(name, constructor, instance=None, *, preferred=False)
87 88 89 90 91 92 93

   Register the browser type *name*.  Once a browser type is registered, the
   :func:`get` function can return a controller for that browser type.  If
   *instance* is not provided, or is ``None``, *constructor* will be called without
   parameters to create an instance when needed.  If *instance* is provided,
   *constructor* will never be called, and may be ``None``.

94
   Setting *preferred* to ``True`` makes this browser a preferred result for
95
   a :func:`get` call with no argument.  Otherwise, this entry point is only
96 97 98
   useful if you plan to either set the :envvar:`BROWSER` variable or call
   :func:`get` with a nonempty argument matching the name of a handler you
   declare.
99

100 101 102
   .. versionchanged:: 3.7
      *preferred* keyword-only parameter was added.

103 104 105 106
A number of browser types are predefined.  This table gives the type names that
may be passed to the :func:`get` function and the corresponding instantiations
for the controller classes, all defined in this module.

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
+------------------------+-----------------------------------------+-------+
| Type Name              | Class Name                              | Notes |
+========================+=========================================+=======+
| ``'mozilla'``          | :class:`Mozilla('mozilla')`             |       |
+------------------------+-----------------------------------------+-------+
| ``'firefox'``          | :class:`Mozilla('mozilla')`             |       |
+------------------------+-----------------------------------------+-------+
| ``'netscape'``         | :class:`Mozilla('netscape')`            |       |
+------------------------+-----------------------------------------+-------+
| ``'galeon'``           | :class:`Galeon('galeon')`               |       |
+------------------------+-----------------------------------------+-------+
| ``'epiphany'``         | :class:`Galeon('epiphany')`             |       |
+------------------------+-----------------------------------------+-------+
| ``'skipstone'``        | :class:`BackgroundBrowser('skipstone')` |       |
+------------------------+-----------------------------------------+-------+
| ``'kfmclient'``        | :class:`Konqueror()`                    | \(1)  |
+------------------------+-----------------------------------------+-------+
| ``'konqueror'``        | :class:`Konqueror()`                    | \(1)  |
+------------------------+-----------------------------------------+-------+
| ``'kfm'``              | :class:`Konqueror()`                    | \(1)  |
+------------------------+-----------------------------------------+-------+
| ``'mosaic'``           | :class:`BackgroundBrowser('mosaic')`    |       |
+------------------------+-----------------------------------------+-------+
| ``'opera'``            | :class:`Opera()`                        |       |
+------------------------+-----------------------------------------+-------+
| ``'grail'``            | :class:`Grail()`                        |       |
+------------------------+-----------------------------------------+-------+
| ``'links'``            | :class:`GenericBrowser('links')`        |       |
+------------------------+-----------------------------------------+-------+
| ``'elinks'``           | :class:`Elinks('elinks')`               |       |
+------------------------+-----------------------------------------+-------+
| ``'lynx'``             | :class:`GenericBrowser('lynx')`         |       |
+------------------------+-----------------------------------------+-------+
| ``'w3m'``              | :class:`GenericBrowser('w3m')`          |       |
+------------------------+-----------------------------------------+-------+
| ``'windows-default'``  | :class:`WindowsDefault`                 | \(2)  |
+------------------------+-----------------------------------------+-------+
144
| ``'macosx'``           | :class:`MacOSX('default')`              | \(3)  |
145
+------------------------+-----------------------------------------+-------+
146
| ``'safari'``           | :class:`MacOSX('safari')`               | \(3)  |
Sandro Tosi's avatar
Sandro Tosi committed
147
+------------------------+-----------------------------------------+-------+
148 149 150 151 152 153 154 155
| ``'google-chrome'``    | :class:`Chrome('google-chrome')`        |       |
+------------------------+-----------------------------------------+-------+
| ``'chrome'``           | :class:`Chrome('chrome')`               |       |
+------------------------+-----------------------------------------+-------+
| ``'chromium'``         | :class:`Chromium('chromium')`           |       |
+------------------------+-----------------------------------------+-------+
| ``'chromium-browser'`` | :class:`Chromium('chromium-browser')`   |       |
+------------------------+-----------------------------------------+-------+
156 157 158 159 160 161 162 163 164 165 166 167 168 169

Notes:

(1)
   "Konqueror" is the file manager for the KDE desktop environment for Unix, and
   only makes sense to use if KDE is running.  Some way of reliably detecting KDE
   would be nice; the :envvar:`KDEDIR` variable is not sufficient.  Note also that
   the name "kfm" is used even when using the :program:`konqueror` command with KDE
   2 --- the implementation selects the best strategy for running Konqueror.

(2)
   Only on Windows platforms.

(3)
170
   Only on Mac OS X platform.
171

172 173 174
.. versionadded:: 3.3
   Support for Chrome/Chromium has been added.

175 176
Here are some simple examples::

177
   url = 'http://docs.python.org/'
178

179
   # Open URL in a new tab, if a browser window is already open.
180
   webbrowser.open_new_tab(url)
181 182 183 184 185 186 187 188 189 190

   # Open URL in new window, raising the window if possible.
   webbrowser.open_new(url)


.. _browser-controllers:

Browser Controller Objects
--------------------------

Benjamin Peterson's avatar
Benjamin Peterson committed
191 192
Browser controllers provide these methods which parallel three of the
module-level convenience functions:
193 194


195
.. method:: controller.open(url, new=0, autoraise=True)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

   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.


.. method:: 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
   :func:`open_new`.


.. method:: controller.open_new_tab(url)

   Open *url* in a new page ("tab") of the browser handled by this controller, if
   possible, otherwise equivalent to :func:`open_new`.
213 214 215 216 217 218


.. rubric:: Footnotes

.. [1] Executables named here without a full path will be searched in the
       directories given in the :envvar:`PATH` environment variable.