1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<span id="webbrowser-convenient-web-browser-controller"></span><h1>webbrowser — Convenient web-browser controller</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/webbrowser.py">Lib/webbrowser.py</a></p> <p>The <a class="reference internal" href="#module-webbrowser" title="webbrowser: Easy-to-use controller for web browsers."><code>webbrowser</code></a> module provides a high-level interface to allow displaying web-based documents to users. Under most circumstances, simply calling the <a class="reference internal" href="#webbrowser.open" title="webbrowser.open"><code>open()</code></a> function from this module will do the right thing.</p> <p>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.</p> <p>If the environment variable <span class="target" id="index-0"></span><code>BROWSER</code> exists, it is interpreted as the <a class="reference internal" href="os#os.pathsep" title="os.pathsep"><code>os.pathsep</code></a>-separated list of browsers to try ahead of the platform defaults. When the value of a list part contains the string <code>%s</code>, then it is interpreted as a literal browser command line to be used with the argument URL substituted for <code>%s</code>; if the part does not contain <code>%s</code>, it is simply interpreted as the name of the browser to launch. <a class="footnote-reference brackets" href="#id2" id="id1">1</a></p> <p>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.</p> <p>The script <strong class="program">webbrowser</strong> can be used as a command-line interface for the module. It accepts a URL as the argument. It accepts the following optional parameters: <code>-n</code> opens the URL in a new browser window, if possible; <code>-t</code> opens the URL in a new browser page (“tab”). The options are, naturally, mutually exclusive. Usage example:</p> <pre data-language="python">python -m webbrowser -t "https://www.python.org"
</pre> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p> <p>This module does not work or is not available on WebAssembly platforms <code>wasm32-emscripten</code> and <code>wasm32-wasi</code>. See <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> <p>The following exception is defined:</p> <dl class="py exception"> <dt class="sig sig-object py" id="webbrowser.Error">
<code>exception webbrowser.Error</code> </dt> <dd>
<p>Exception raised when a browser control error occurs.</p> </dd>
</dl> <p>The following functions are defined:</p> <dl class="py function"> <dt class="sig sig-object py" id="webbrowser.open">
<code>webbrowser.open(url, new=0, autoraise=True)</code> </dt> <dd>
<p>Display <em>url</em> using the default browser. If <em>new</em> is 0, the <em>url</em> is opened in the same browser window if possible. If <em>new</em> is 1, a new browser window is opened if possible. If <em>new</em> is 2, a new browser page (“tab”) is opened if possible. If <em>autoraise</em> is <code>True</code>, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).</p> <p>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.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>webbrowser.open</code> with argument <code>url</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="webbrowser.open_new">
<code>webbrowser.open_new(url)</code> </dt> <dd>
<p>Open <em>url</em> in a new window of the default browser, if possible, otherwise, open <em>url</em> in the only browser window.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="webbrowser.open_new_tab">
<code>webbrowser.open_new_tab(url)</code> </dt> <dd>
<p>Open <em>url</em> in a new page (“tab”) of the default browser, if possible, otherwise equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code>open_new()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="webbrowser.get">
<code>webbrowser.get(using=None)</code> </dt> <dd>
<p>Return a controller object for the browser type <em>using</em>. If <em>using</em> is <code>None</code>, return a controller for a default browser appropriate to the caller’s environment.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="webbrowser.register">
<code>webbrowser.register(name, constructor, instance=None, *, preferred=False)</code> </dt> <dd>
<p>Register the browser type <em>name</em>. Once a browser type is registered, the <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code>get()</code></a> function can return a controller for that browser type. If <em>instance</em> is not provided, or is <code>None</code>, <em>constructor</em> will be called without parameters to create an instance when needed. If <em>instance</em> is provided, <em>constructor</em> will never be called, and may be <code>None</code>.</p> <p>Setting <em>preferred</em> to <code>True</code> makes this browser a preferred result for a <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code>get()</code></a> call with no argument. Otherwise, this entry point is only useful if you plan to either set the <span class="target" id="index-1"></span><code>BROWSER</code> variable or call <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code>get()</code></a> with a nonempty argument matching the name of a handler you declare.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span><em>preferred</em> keyword-only parameter was added.</p> </div> </dd>
</dl> <p>A number of browser types are predefined. This table gives the type names that may be passed to the <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code>get()</code></a> function and the corresponding instantiations for the controller classes, all defined in this module.</p> <table class="docutils align-default"> <thead> <tr>
<th class="head"><p>Type Name</p></th> <th class="head"><p>Class Name</p></th> <th class="head"><p>Notes</p></th> </tr> </thead> <tr>
<td><p><code>'mozilla'</code></p></td> <td><p><code>Mozilla('mozilla')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'firefox'</code></p></td> <td><p><code>Mozilla('mozilla')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'epiphany'</code></p></td> <td><p><code>Epiphany('epiphany')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'kfmclient'</code></p></td> <td><p><code>Konqueror()</code></p></td> <td><p>(1)</p></td> </tr> <tr>
<td><p><code>'konqueror'</code></p></td> <td><p><code>Konqueror()</code></p></td> <td><p>(1)</p></td> </tr> <tr>
<td><p><code>'kfm'</code></p></td> <td><p><code>Konqueror()</code></p></td> <td><p>(1)</p></td> </tr> <tr>
<td><p><code>'opera'</code></p></td> <td><p><code>Opera()</code></p></td> <td></td> </tr> <tr>
<td><p><code>'links'</code></p></td> <td><p><code>GenericBrowser('links')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'elinks'</code></p></td> <td><p><code>Elinks('elinks')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'lynx'</code></p></td> <td><p><code>GenericBrowser('lynx')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'w3m'</code></p></td> <td><p><code>GenericBrowser('w3m')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'windows-default'</code></p></td> <td><p><code>WindowsDefault</code></p></td> <td><p>(2)</p></td> </tr> <tr>
<td><p><code>'macosx'</code></p></td> <td><p><code>MacOSXOSAScript('default')</code></p></td> <td><p>(3)</p></td> </tr> <tr>
<td><p><code>'safari'</code></p></td> <td><p><code>MacOSXOSAScript('safari')</code></p></td> <td><p>(3)</p></td> </tr> <tr>
<td><p><code>'google-chrome'</code></p></td> <td><p><code>Chrome('google-chrome')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'chrome'</code></p></td> <td><p><code>Chrome('chrome')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'chromium'</code></p></td> <td><p><code>Chromium('chromium')</code></p></td> <td></td> </tr> <tr>
<td><p><code>'chromium-browser'</code></p></td> <td><p><code>Chromium('chromium-browser')</code></p></td> <td></td> </tr> </table> <p>Notes:</p> <ol class="arabic simple"> <li>“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 <span class="target" id="index-2"></span><code>KDEDIR</code> variable is not sufficient. Note also that the name “kfm” is used even when using the <strong class="program">konqueror</strong> command with KDE 2 — the implementation selects the best strategy for running Konqueror.</li> <li>Only on Windows platforms.</li> <li>Only on macOS platform.</li> </ol> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Support for Chrome/Chromium has been added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Support for several obsolete browsers has been removed. Removed browsers include Grail, Mosaic, Netscape, Galeon, Skipstone, Iceape, and Firefox versions 35 and below.</p> </div> <div class="deprecated-removed"> <p><span class="versionmodified">Deprecated since version 3.11, will be removed in version 3.13: </span><code>MacOSX</code> is deprecated, use <code>MacOSXOSAScript</code> instead.</p> </div> <p>Here are some simple examples:</p> <pre data-language="python">url = 'https://docs.python.org/'
# Open URL in a new tab, if a browser window is already open.
webbrowser.open_new_tab(url)
# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)
</pre> <section id="browser-controller-objects"> <span id="browser-controllers"></span><h2>Browser Controller Objects</h2> <p>Browser controllers provide these methods which parallel three of the module-level convenience functions:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="webbrowser.name">
<code>webbrowser.name</code> </dt> <dd>
<p>System-dependent name for the browser.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="webbrowser.controller.open">
<code>controller.open(url, new=0, autoraise=True)</code> </dt> <dd>
<p>Display <em>url</em> using the browser handled by this controller. If <em>new</em> is 1, a new browser window is opened if possible. If <em>new</em> is 2, a new browser page (“tab”) is opened if possible.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="webbrowser.controller.open_new">
<code>controller.open_new(url)</code> </dt> <dd>
<p>Open <em>url</em> in a new window of the browser handled by this controller, if possible, otherwise, open <em>url</em> in the only browser window. Alias <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code>open_new()</code></a>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="webbrowser.controller.open_new_tab">
<code>controller.open_new_tab(url)</code> </dt> <dd>
<p>Open <em>url</em> in a new page (“tab”) of the browser handled by this controller, if possible, otherwise equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code>open_new()</code></a>.</p> </dd>
</dl> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id2">
<code>1</code> </dt> <dd>
<p>Executables named here without a full path will be searched in the directories given in the <span class="target" id="index-3"></span><code>PATH</code> environment variable.</p> </dd> </dl> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/webbrowser.html" class="_attribution-link">https://docs.python.org/3.12/library/webbrowser.html</a>
</p>
</div>
|