Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`webbrowser` --- Convenient Web-browser controller |
| 2 | ======================================================= |
| 3 | |
| 4 | .. module:: webbrowser |
| 5 | :synopsis: Easy-to-use controller for Web browsers. |
| 6 | .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/webbrowser.py` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
| 13 | The :mod:`webbrowser` module provides a high-level interface to allow displaying |
| 14 | Web-based documents to users. Under most circumstances, simply calling the |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 15 | :func:`.open` function from this module will do the right thing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
| 17 | Under Unix, graphical browsers are preferred under X11, but text-mode browsers |
| 18 | will be used if graphical browsers are not available or an X11 display isn't |
| 19 | available. If text-mode browsers are used, the calling process will block until |
| 20 | the user exits the browser. |
| 21 | |
| 22 | If the environment variable :envvar:`BROWSER` exists, it is interpreted to |
Ezio Melotti | 579c29b | 2009-12-24 03:23:23 +0000 | [diff] [blame] | 23 | override the platform default list of browsers, as a :data:`os.pathsep`-separated |
| 24 | list of browsers to try in order. When the value of a list part contains the |
| 25 | string ``%s``, then it is interpreted as a literal browser command line to be |
| 26 | used with the argument URL substituted for ``%s``; if the part does not contain |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 27 | ``%s``, it is simply interpreted as the name of the browser to launch. [1]_ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | |
| 29 | For non-Unix platforms, or when a remote browser is available on Unix, the |
| 30 | controlling process will not wait for the user to finish with the browser, but |
| 31 | allow the remote browser to maintain its own windows on the display. If remote |
| 32 | browsers are not available on Unix, the controlling process will launch a new |
| 33 | browser and wait. |
| 34 | |
| 35 | The script :program:`webbrowser` can be used as a command-line interface for the |
| 36 | module. It accepts an URL as the argument. It accepts the following optional |
Éric Araujo | 713d303 | 2010-11-18 16:38:46 +0000 | [diff] [blame] | 37 | parameters: ``-n`` opens the URL in a new browser window, if possible; |
| 38 | ``-t`` opens the URL in a new browser page ("tab"). The options are, |
Sandro Tosi | c7b0e21 | 2012-08-12 17:34:00 +0200 | [diff] [blame] | 39 | naturally, mutually exclusive. Usage example:: |
| 40 | |
| 41 | python -m webbrowser -t "http://www.python.org" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
| 43 | The following exception is defined: |
| 44 | |
| 45 | |
| 46 | .. exception:: Error |
| 47 | |
| 48 | Exception raised when a browser control error occurs. |
| 49 | |
| 50 | The following functions are defined: |
| 51 | |
| 52 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 53 | .. function:: open(url, new=0, autoraise=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 55 | Display *url* using the default browser. If *new* is 0, the *url* is opened |
| 56 | in the same browser window if possible. If *new* is 1, a new browser window |
| 57 | is opened if possible. If *new* is 2, a new browser page ("tab") is opened |
| 58 | if possible. If *autoraise* is ``True``, the window is raised if possible |
| 59 | (note that under many window managers this will occur regardless of the |
| 60 | setting of this variable). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 62 | Note that on some platforms, trying to open a filename using this function, |
| 63 | may work and start the operating system's associated program. However, this |
| 64 | is neither supported nor portable. |
| 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | .. function:: open_new(url) |
| 68 | |
| 69 | Open *url* in a new window of the default browser, if possible, otherwise, open |
| 70 | *url* in the only browser window. |
| 71 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | .. function:: open_new_tab(url) |
| 73 | |
| 74 | Open *url* in a new page ("tab") of the default browser, if possible, otherwise |
| 75 | equivalent to :func:`open_new`. |
| 76 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 78 | .. function:: get(using=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 80 | Return a controller object for the browser type *using*. If *using* is |
| 81 | ``None``, return a controller for a default browser appropriate to the |
| 82 | caller's environment. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 85 | .. function:: register(name, constructor, instance=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | Register the browser type *name*. Once a browser type is registered, the |
| 88 | :func:`get` function can return a controller for that browser type. If |
| 89 | *instance* is not provided, or is ``None``, *constructor* will be called without |
| 90 | parameters to create an instance when needed. If *instance* is provided, |
| 91 | *constructor* will never be called, and may be ``None``. |
| 92 | |
| 93 | This entry point is only useful if you plan to either set the :envvar:`BROWSER` |
| 94 | variable or call :func:`get` with a nonempty argument matching the name of a |
| 95 | handler you declare. |
| 96 | |
| 97 | A number of browser types are predefined. This table gives the type names that |
| 98 | may be passed to the :func:`get` function and the corresponding instantiations |
| 99 | for the controller classes, all defined in this module. |
| 100 | |
Senthil Kumaran | c909599 | 2011-12-21 22:31:36 +0800 | [diff] [blame] | 101 | +------------------------+-----------------------------------------+-------+ |
| 102 | | Type Name | Class Name | Notes | |
| 103 | +========================+=========================================+=======+ |
| 104 | | ``'mozilla'`` | :class:`Mozilla('mozilla')` | | |
| 105 | +------------------------+-----------------------------------------+-------+ |
| 106 | | ``'firefox'`` | :class:`Mozilla('mozilla')` | | |
| 107 | +------------------------+-----------------------------------------+-------+ |
| 108 | | ``'netscape'`` | :class:`Mozilla('netscape')` | | |
| 109 | +------------------------+-----------------------------------------+-------+ |
| 110 | | ``'galeon'`` | :class:`Galeon('galeon')` | | |
| 111 | +------------------------+-----------------------------------------+-------+ |
| 112 | | ``'epiphany'`` | :class:`Galeon('epiphany')` | | |
| 113 | +------------------------+-----------------------------------------+-------+ |
| 114 | | ``'skipstone'`` | :class:`BackgroundBrowser('skipstone')` | | |
| 115 | +------------------------+-----------------------------------------+-------+ |
| 116 | | ``'kfmclient'`` | :class:`Konqueror()` | \(1) | |
| 117 | +------------------------+-----------------------------------------+-------+ |
| 118 | | ``'konqueror'`` | :class:`Konqueror()` | \(1) | |
| 119 | +------------------------+-----------------------------------------+-------+ |
| 120 | | ``'kfm'`` | :class:`Konqueror()` | \(1) | |
| 121 | +------------------------+-----------------------------------------+-------+ |
| 122 | | ``'mosaic'`` | :class:`BackgroundBrowser('mosaic')` | | |
| 123 | +------------------------+-----------------------------------------+-------+ |
| 124 | | ``'opera'`` | :class:`Opera()` | | |
| 125 | +------------------------+-----------------------------------------+-------+ |
| 126 | | ``'grail'`` | :class:`Grail()` | | |
| 127 | +------------------------+-----------------------------------------+-------+ |
| 128 | | ``'links'`` | :class:`GenericBrowser('links')` | | |
| 129 | +------------------------+-----------------------------------------+-------+ |
| 130 | | ``'elinks'`` | :class:`Elinks('elinks')` | | |
| 131 | +------------------------+-----------------------------------------+-------+ |
| 132 | | ``'lynx'`` | :class:`GenericBrowser('lynx')` | | |
| 133 | +------------------------+-----------------------------------------+-------+ |
| 134 | | ``'w3m'`` | :class:`GenericBrowser('w3m')` | | |
| 135 | +------------------------+-----------------------------------------+-------+ |
| 136 | | ``'windows-default'`` | :class:`WindowsDefault` | \(2) | |
| 137 | +------------------------+-----------------------------------------+-------+ |
Ned Deily | 05fac02 | 2012-04-18 12:55:57 -0700 | [diff] [blame] | 138 | | ``'macosx'`` | :class:`MacOSX('default')` | \(3) | |
Senthil Kumaran | c909599 | 2011-12-21 22:31:36 +0800 | [diff] [blame] | 139 | +------------------------+-----------------------------------------+-------+ |
Ned Deily | 05fac02 | 2012-04-18 12:55:57 -0700 | [diff] [blame] | 140 | | ``'safari'`` | :class:`MacOSX('safari')` | \(3) | |
Sandro Tosi | dc60f94 | 2012-03-31 17:44:33 +0200 | [diff] [blame] | 141 | +------------------------+-----------------------------------------+-------+ |
Senthil Kumaran | c909599 | 2011-12-21 22:31:36 +0800 | [diff] [blame] | 142 | | ``'google-chrome'`` | :class:`Chrome('google-chrome')` | | |
| 143 | +------------------------+-----------------------------------------+-------+ |
| 144 | | ``'chrome'`` | :class:`Chrome('chrome')` | | |
| 145 | +------------------------+-----------------------------------------+-------+ |
| 146 | | ``'chromium'`` | :class:`Chromium('chromium')` | | |
| 147 | +------------------------+-----------------------------------------+-------+ |
| 148 | | ``'chromium-browser'`` | :class:`Chromium('chromium-browser')` | | |
| 149 | +------------------------+-----------------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
| 151 | Notes: |
| 152 | |
| 153 | (1) |
| 154 | "Konqueror" is the file manager for the KDE desktop environment for Unix, and |
| 155 | only makes sense to use if KDE is running. Some way of reliably detecting KDE |
| 156 | would be nice; the :envvar:`KDEDIR` variable is not sufficient. Note also that |
| 157 | the name "kfm" is used even when using the :program:`konqueror` command with KDE |
| 158 | 2 --- the implementation selects the best strategy for running Konqueror. |
| 159 | |
| 160 | (2) |
| 161 | Only on Windows platforms. |
| 162 | |
| 163 | (3) |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 164 | Only on Mac OS X platform. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
Ezio Melotti | 25d1bdc | 2012-01-18 02:32:07 +0200 | [diff] [blame] | 166 | .. versionadded:: 3.3 |
| 167 | Support for Chrome/Chromium has been added. |
| 168 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | Here are some simple examples:: |
| 170 | |
Ezio Melotti | 25d1bdc | 2012-01-18 02:32:07 +0200 | [diff] [blame] | 171 | url = 'http://docs.python.org/' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 173 | # Open URL in a new tab, if a browser window is already open. |
Ezio Melotti | 25d1bdc | 2012-01-18 02:32:07 +0200 | [diff] [blame] | 174 | webbrowser.open_new_tab(url) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 175 | |
| 176 | # Open URL in new window, raising the window if possible. |
| 177 | webbrowser.open_new(url) |
| 178 | |
| 179 | |
| 180 | .. _browser-controllers: |
| 181 | |
| 182 | Browser Controller Objects |
| 183 | -------------------------- |
| 184 | |
Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 185 | Browser controllers provide these methods which parallel three of the |
| 186 | module-level convenience functions: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 189 | .. method:: controller.open(url, new=0, autoraise=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | Display *url* using the browser handled by this controller. If *new* is 1, a new |
| 192 | browser window is opened if possible. If *new* is 2, a new browser page ("tab") |
| 193 | is opened if possible. |
| 194 | |
| 195 | |
| 196 | .. method:: controller.open_new(url) |
| 197 | |
| 198 | Open *url* in a new window of the browser handled by this controller, if |
| 199 | possible, otherwise, open *url* in the only browser window. Alias |
| 200 | :func:`open_new`. |
| 201 | |
| 202 | |
| 203 | .. method:: controller.open_new_tab(url) |
| 204 | |
| 205 | Open *url* in a new page ("tab") of the browser handled by this controller, if |
| 206 | possible, otherwise equivalent to :func:`open_new`. |
Georg Brandl | 8a1e4c4 | 2009-05-25 21:13:36 +0000 | [diff] [blame] | 207 | |
| 208 | |
| 209 | .. rubric:: Footnotes |
| 210 | |
| 211 | .. [1] Executables named here without a full path will be searched in the |
| 212 | directories given in the :envvar:`PATH` environment variable. |