1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>21.1. webbrowser — Convenient Web-browser controller — Python 3.5.2 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.2 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.2 documentation" href="../contents.html" />
33
<link rel="up" title="21. Internet Protocols and Support" href="internet.html" />
34
<link rel="next" title="21.2. cgi — Common Gateway Interface support" href="cgi.html" />
35
<link rel="prev" title="21. Internet Protocols and Support" href="internet.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
38
<script type="text/javascript" src="../_static/copybutton.js"></script>
39
<script type="text/javascript" src="../_static/version_switch.js"></script>
45
<body role="document">
46
<div class="related" role="navigation" aria-label="related navigation">
49
<li class="right" style="margin-right: 10px">
50
<a href="../genindex.html" title="General Index"
51
accesskey="I">index</a></li>
53
<a href="../py-modindex.html" title="Python Module Index"
56
<a href="cgi.html" title="21.2. cgi — Common Gateway Interface support"
57
accesskey="N">next</a> |</li>
59
<a href="internet.html" title="21. Internet Protocols and Support"
60
accesskey="P">previous</a> |</li>
61
<li><img src="../_static/py.png" alt=""
62
style="vertical-align: middle; margin-top: -1px"/></li>
63
<li><a href="https://www.python.org/">Python</a> »</li>
65
<span class="version_switcher_placeholder">3.5.2</span>
66
<a href="../index.html">Documentation </a> »
69
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
70
<li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> »</li>
74
<div class="inline-search" style="display: none" role="search">
75
<form class="inline-search" action="../search.html" method="get">
76
<input placeholder="Quick search" type="text" name="q" />
77
<input type="submit" value="Go" />
78
<input type="hidden" name="check_keywords" value="yes" />
79
<input type="hidden" name="area" value="default" />
82
<script type="text/javascript">$('.inline-search').show(0);</script>
89
<div class="document">
90
<div class="documentwrapper">
91
<div class="bodywrapper">
92
<div class="body" role="main">
94
<div class="section" id="module-webbrowser">
95
<span id="webbrowser-convenient-web-browser-controller"></span><h1>21.1. <a class="reference internal" href="#module-webbrowser" title="webbrowser: Easy-to-use controller for Web browsers."><code class="xref py py-mod docutils literal"><span class="pre">webbrowser</span></code></a> — Convenient Web-browser controller<a class="headerlink" href="#module-webbrowser" title="Permalink to this headline">¶</a></h1>
96
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/webbrowser.py">Lib/webbrowser.py</a></p>
97
<hr class="docutils" />
98
<p>The <a class="reference internal" href="#module-webbrowser" title="webbrowser: Easy-to-use controller for Web browsers."><code class="xref py py-mod docutils literal"><span class="pre">webbrowser</span></code></a> module provides a high-level interface to allow displaying
99
Web-based documents to users. Under most circumstances, simply calling the
100
<a class="reference internal" href="#webbrowser.open" title="webbrowser.open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> function from this module will do the right thing.</p>
101
<p>Under Unix, graphical browsers are preferred under X11, but text-mode browsers
102
will be used if graphical browsers are not available or an X11 display isn’t
103
available. If text-mode browsers are used, the calling process will block until
104
the user exits the browser.</p>
105
<p>If the environment variable <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal"><span class="pre">BROWSER</span></code> exists, it is interpreted as the
106
<a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal"><span class="pre">os.pathsep</span></code></a>-separated list of browsers to try ahead of the platform
107
defaults. When the value of a list part contains the string <code class="docutils literal"><span class="pre">%s</span></code>, then it is
108
interpreted as a literal browser command line to be used with the argument URL
109
substituted for <code class="docutils literal"><span class="pre">%s</span></code>; if the part does not contain <code class="docutils literal"><span class="pre">%s</span></code>, it is simply
110
interpreted as the name of the browser to launch. <a class="footnote-reference" href="#id2" id="id1">[1]</a></p>
111
<p>For non-Unix platforms, or when a remote browser is available on Unix, the
112
controlling process will not wait for the user to finish with the browser, but
113
allow the remote browser to maintain its own windows on the display. If remote
114
browsers are not available on Unix, the controlling process will launch a new
115
browser and wait.</p>
116
<p>The script <strong class="program">webbrowser</strong> can be used as a command-line interface for the
117
module. It accepts a URL as the argument. It accepts the following optional
118
parameters: <code class="docutils literal"><span class="pre">-n</span></code> opens the URL in a new browser window, if possible;
119
<code class="docutils literal"><span class="pre">-t</span></code> opens the URL in a new browser page (“tab”). The options are,
120
naturally, mutually exclusive. Usage example:</p>
121
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">webbrowser</span> <span class="o">-</span><span class="n">t</span> <span class="s2">"http://www.python.org"</span>
124
<p>The following exception is defined:</p>
125
<dl class="exception">
126
<dt id="webbrowser.Error">
127
<em class="property">exception </em><code class="descclassname">webbrowser.</code><code class="descname">Error</code><a class="headerlink" href="#webbrowser.Error" title="Permalink to this definition">¶</a></dt>
128
<dd><p>Exception raised when a browser control error occurs.</p>
131
<p>The following functions are defined:</p>
132
<dl class="function">
133
<dt id="webbrowser.open">
134
<code class="descclassname">webbrowser.</code><code class="descname">open</code><span class="sig-paren">(</span><em>url</em>, <em>new=0</em>, <em>autoraise=True</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open" title="Permalink to this definition">¶</a></dt>
135
<dd><p>Display <em>url</em> using the default browser. If <em>new</em> is 0, the <em>url</em> is opened
136
in the same browser window if possible. If <em>new</em> is 1, a new browser window
137
is opened if possible. If <em>new</em> is 2, a new browser page (“tab”) is opened
138
if possible. If <em>autoraise</em> is <code class="docutils literal"><span class="pre">True</span></code>, the window is raised if possible
139
(note that under many window managers this will occur regardless of the
140
setting of this variable).</p>
141
<p>Note that on some platforms, trying to open a filename using this function,
142
may work and start the operating system’s associated program. However, this
143
is neither supported nor portable.</p>
146
<dl class="function">
147
<dt id="webbrowser.open_new">
148
<code class="descclassname">webbrowser.</code><code class="descname">open_new</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open_new" title="Permalink to this definition">¶</a></dt>
149
<dd><p>Open <em>url</em> in a new window of the default browser, if possible, otherwise, open
150
<em>url</em> in the only browser window.</p>
153
<dl class="function">
154
<dt id="webbrowser.open_new_tab">
155
<code class="descclassname">webbrowser.</code><code class="descname">open_new_tab</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open_new_tab" title="Permalink to this definition">¶</a></dt>
156
<dd><p>Open <em>url</em> in a new page (“tab”) of the default browser, if possible, otherwise
157
equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal"><span class="pre">open_new()</span></code></a>.</p>
160
<dl class="function">
161
<dt id="webbrowser.get">
162
<code class="descclassname">webbrowser.</code><code class="descname">get</code><span class="sig-paren">(</span><em>using=None</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.get" title="Permalink to this definition">¶</a></dt>
163
<dd><p>Return a controller object for the browser type <em>using</em>. If <em>using</em> is
164
<code class="docutils literal"><span class="pre">None</span></code>, return a controller for a default browser appropriate to the
165
caller’s environment.</p>
168
<dl class="function">
169
<dt id="webbrowser.register">
170
<code class="descclassname">webbrowser.</code><code class="descname">register</code><span class="sig-paren">(</span><em>name</em>, <em>constructor</em>, <em>instance=None</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.register" title="Permalink to this definition">¶</a></dt>
171
<dd><p>Register the browser type <em>name</em>. Once a browser type is registered, the
172
<a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal"><span class="pre">get()</span></code></a> function can return a controller for that browser type. If
173
<em>instance</em> is not provided, or is <code class="docutils literal"><span class="pre">None</span></code>, <em>constructor</em> will be called without
174
parameters to create an instance when needed. If <em>instance</em> is provided,
175
<em>constructor</em> will never be called, and may be <code class="docutils literal"><span class="pre">None</span></code>.</p>
176
<p>This entry point is only useful if you plan to either set the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal"><span class="pre">BROWSER</span></code>
177
variable or call <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal"><span class="pre">get()</span></code></a> with a nonempty argument matching the name of a
178
handler you declare.</p>
181
<p>A number of browser types are predefined. This table gives the type names that
182
may be passed to the <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal"><span class="pre">get()</span></code></a> function and the corresponding instantiations
183
for the controller classes, all defined in this module.</p>
184
<table border="1" class="docutils">
190
<thead valign="bottom">
191
<tr class="row-odd"><th class="head">Type Name</th>
192
<th class="head">Class Name</th>
193
<th class="head">Notes</th>
197
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'mozilla'</span></code></td>
198
<td><code class="xref py py-class docutils literal"><span class="pre">Mozilla('mozilla')</span></code></td>
201
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'firefox'</span></code></td>
202
<td><code class="xref py py-class docutils literal"><span class="pre">Mozilla('mozilla')</span></code></td>
205
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'netscape'</span></code></td>
206
<td><code class="xref py py-class docutils literal"><span class="pre">Mozilla('netscape')</span></code></td>
209
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'galeon'</span></code></td>
210
<td><code class="xref py py-class docutils literal"><span class="pre">Galeon('galeon')</span></code></td>
213
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'epiphany'</span></code></td>
214
<td><code class="xref py py-class docutils literal"><span class="pre">Galeon('epiphany')</span></code></td>
217
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'skipstone'</span></code></td>
218
<td><code class="xref py py-class docutils literal"><span class="pre">BackgroundBrowser('skipstone')</span></code></td>
221
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'kfmclient'</span></code></td>
222
<td><code class="xref py py-class docutils literal"><span class="pre">Konqueror()</span></code></td>
225
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'konqueror'</span></code></td>
226
<td><code class="xref py py-class docutils literal"><span class="pre">Konqueror()</span></code></td>
229
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'kfm'</span></code></td>
230
<td><code class="xref py py-class docutils literal"><span class="pre">Konqueror()</span></code></td>
233
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'mosaic'</span></code></td>
234
<td><code class="xref py py-class docutils literal"><span class="pre">BackgroundBrowser('mosaic')</span></code></td>
237
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'opera'</span></code></td>
238
<td><code class="xref py py-class docutils literal"><span class="pre">Opera()</span></code></td>
241
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'grail'</span></code></td>
242
<td><code class="xref py py-class docutils literal"><span class="pre">Grail()</span></code></td>
245
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'links'</span></code></td>
246
<td><code class="xref py py-class docutils literal"><span class="pre">GenericBrowser('links')</span></code></td>
249
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'elinks'</span></code></td>
250
<td><code class="xref py py-class docutils literal"><span class="pre">Elinks('elinks')</span></code></td>
253
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'lynx'</span></code></td>
254
<td><code class="xref py py-class docutils literal"><span class="pre">GenericBrowser('lynx')</span></code></td>
257
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'w3m'</span></code></td>
258
<td><code class="xref py py-class docutils literal"><span class="pre">GenericBrowser('w3m')</span></code></td>
261
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'windows-default'</span></code></td>
262
<td><code class="xref py py-class docutils literal"><span class="pre">WindowsDefault</span></code></td>
265
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'macosx'</span></code></td>
266
<td><code class="xref py py-class docutils literal"><span class="pre">MacOSX('default')</span></code></td>
269
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'safari'</span></code></td>
270
<td><code class="xref py py-class docutils literal"><span class="pre">MacOSX('safari')</span></code></td>
273
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'google-chrome'</span></code></td>
274
<td><code class="xref py py-class docutils literal"><span class="pre">Chrome('google-chrome')</span></code></td>
277
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'chrome'</span></code></td>
278
<td><code class="xref py py-class docutils literal"><span class="pre">Chrome('chrome')</span></code></td>
281
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">'chromium'</span></code></td>
282
<td><code class="xref py py-class docutils literal"><span class="pre">Chromium('chromium')</span></code></td>
285
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'chromium-browser'</span></code></td>
286
<td><code class="xref py py-class docutils literal"><span class="pre">Chromium('chromium-browser')</span></code></td>
292
<ol class="arabic simple">
293
<li>“Konqueror” is the file manager for the KDE desktop environment for Unix, and
294
only makes sense to use if KDE is running. Some way of reliably detecting KDE
295
would be nice; the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal"><span class="pre">KDEDIR</span></code> variable is not sufficient. Note also that
296
the name “kfm” is used even when using the <strong class="program">konqueror</strong> command with KDE
297
2 — the implementation selects the best strategy for running Konqueror.</li>
298
<li>Only on Windows platforms.</li>
299
<li>Only on Mac OS X platform.</li>
301
<div class="versionadded">
302
<p><span class="versionmodified">New in version 3.3: </span>Support for Chrome/Chromium has been added.</p>
304
<p>Here are some simple examples:</p>
305
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="n">url</span> <span class="o">=</span> <span class="s1">'http://docs.python.org/'</span>
307
<span class="c1"># Open URL in a new tab, if a browser window is already open.</span>
308
<span class="n">webbrowser</span><span class="o">.</span><span class="n">open_new_tab</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
310
<span class="c1"># Open URL in new window, raising the window if possible.</span>
311
<span class="n">webbrowser</span><span class="o">.</span><span class="n">open_new</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
314
<div class="section" id="browser-controller-objects">
315
<span id="browser-controllers"></span><h2>21.1.1. Browser Controller Objects<a class="headerlink" href="#browser-controller-objects" title="Permalink to this headline">¶</a></h2>
316
<p>Browser controllers provide these methods which parallel three of the
317
module-level convenience functions:</p>
319
<dt id="webbrowser.controller.open">
320
<code class="descclassname">controller.</code><code class="descname">open</code><span class="sig-paren">(</span><em>url</em>, <em>new=0</em>, <em>autoraise=True</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open" title="Permalink to this definition">¶</a></dt>
321
<dd><p>Display <em>url</em> using the browser handled by this controller. If <em>new</em> is 1, a new
322
browser window is opened if possible. If <em>new</em> is 2, a new browser page (“tab”)
323
is opened if possible.</p>
327
<dt id="webbrowser.controller.open_new">
328
<code class="descclassname">controller.</code><code class="descname">open_new</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open_new" title="Permalink to this definition">¶</a></dt>
329
<dd><p>Open <em>url</em> in a new window of the browser handled by this controller, if
330
possible, otherwise, open <em>url</em> in the only browser window. Alias
331
<a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal"><span class="pre">open_new()</span></code></a>.</p>
335
<dt id="webbrowser.controller.open_new_tab">
336
<code class="descclassname">controller.</code><code class="descname">open_new_tab</code><span class="sig-paren">(</span><em>url</em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open_new_tab" title="Permalink to this definition">¶</a></dt>
337
<dd><p>Open <em>url</em> in a new page (“tab”) of the browser handled by this controller, if
338
possible, otherwise equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal"><span class="pre">open_new()</span></code></a>.</p>
341
<p class="rubric">Footnotes</p>
342
<table class="docutils footnote" frame="void" id="id2" rules="none">
343
<colgroup><col class="label" /><col /></colgroup>
345
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Executables named here without a full path will be searched in the
346
directories given in the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal"><span class="pre">PATH</span></code> environment variable.</td></tr>
356
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
357
<div class="sphinxsidebarwrapper">
358
<h3><a href="../contents.html">Table Of Contents</a></h3>
360
<li><a class="reference internal" href="#">21.1. <code class="docutils literal"><span class="pre">webbrowser</span></code> — Convenient Web-browser controller</a><ul>
361
<li><a class="reference internal" href="#browser-controller-objects">21.1.1. Browser Controller Objects</a></li>
366
<h4>Previous topic</h4>
367
<p class="topless"><a href="internet.html"
368
title="previous chapter">21. Internet Protocols and Support</a></p>
370
<p class="topless"><a href="cgi.html"
371
title="next chapter">21.2. <code class="docutils literal"><span class="pre">cgi</span></code> — Common Gateway Interface support</a></p>
372
<div role="note" aria-label="source link">
374
<ul class="this-page-menu">
375
<li><a href="../bugs.html">Report a Bug</a></li>
376
<li><a href="../_sources/library/webbrowser.txt"
377
rel="nofollow">Show Source</a></li>
382
<div class="clearer"></div>
384
<div class="related" role="navigation" aria-label="related navigation">
387
<li class="right" style="margin-right: 10px">
388
<a href="../genindex.html" title="General Index"
391
<a href="../py-modindex.html" title="Python Module Index"
394
<a href="cgi.html" title="21.2. cgi — Common Gateway Interface support"
397
<a href="internet.html" title="21. Internet Protocols and Support"
399
<li><img src="../_static/py.png" alt=""
400
style="vertical-align: middle; margin-top: -1px"/></li>
401
<li><a href="https://www.python.org/">Python</a> »</li>
403
<span class="version_switcher_placeholder">3.5.2</span>
404
<a href="../index.html">Documentation </a> »
407
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
408
<li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> »</li>
412
<div class="inline-search" style="display: none" role="search">
413
<form class="inline-search" action="../search.html" method="get">
414
<input placeholder="Quick search" type="text" name="q" />
415
<input type="submit" value="Go" />
416
<input type="hidden" name="check_keywords" value="yes" />
417
<input type="hidden" name="area" value="default" />
420
<script type="text/javascript">$('.inline-search').show(0);</script>
427
© <a href="../copyright.html">Copyright</a> 2001-2016, Python Software Foundation.
429
The Python Software Foundation is a non-profit corporation.
430
<a href="https://www.python.org/psf/donations/">Please donate.</a>
432
Last updated on Sep 23, 2016.
433
<a href="../bugs.html">Found a bug</a>?
435
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'