gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

585 lines
37KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="webbrowser — Convenient web-browser controller" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/webbrowser.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/webbrowser.py The webbrowser module provides a high-level interface to allow displaying web-based documents to users. Under most circumstances, simply calling the open() function f..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/webbrowser.py The webbrowser module provides a high-level interface to allow displaying web-based documents to users. Under most circumstances, simply calling the open() function f..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>webbrowser — Convenient web-browser controller &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="wsgiref — WSGI Utilities and Reference Implementation" href="wsgiref.html" />
  33. <link rel="prev" title="Internet Protocols and Support" href="internet.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/webbrowser.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code> — Convenient web-browser controller</a><ul>
  86. <li><a class="reference internal" href="#browser-controller-objects">Browser Controller Objects</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="internet.html"
  94. title="previous chapter">Internet Protocols and Support</a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="wsgiref.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code> — WSGI Utilities and Reference Implementation</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/webbrowser.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="wsgiref.html" title="wsgiref — WSGI Utilities and Reference Implementation"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="internet.html" title="Internet Protocols and Support"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code> — Convenient web-browser controller</a></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="module-webbrowser">
  169. <span id="webbrowser-convenient-web-browser-controller"></span><h1><a class="reference internal" href="#module-webbrowser" title="webbrowser: Easy-to-use controller for web browsers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code></a> — Convenient web-browser controller<a class="headerlink" href="#module-webbrowser" title="Link to this heading">¶</a></h1>
  170. <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>
  171. <hr class="docutils" />
  172. <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 notranslate"><span class="pre">webbrowser</span></code></a> module provides a high-level interface to allow displaying
  173. web-based documents to users. Under most circumstances, simply calling the
  174. <a class="reference internal" href="#webbrowser.open" title="webbrowser.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function from this module will do the right thing.</p>
  175. <p>Under Unix, graphical browsers are preferred under X11, but text-mode browsers
  176. will be used if graphical browsers are not available or an X11 display isn’t
  177. available. If text-mode browsers are used, the calling process will block until
  178. the user exits the browser.</p>
  179. <p>If the environment variable <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">BROWSER</span></code> exists, it is interpreted as the
  180. <a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a>-separated list of browsers to try ahead of the platform
  181. defaults. When the value of a list part contains the string <code class="docutils literal notranslate"><span class="pre">%s</span></code>, then it is
  182. interpreted as a literal browser command line to be used with the argument URL
  183. substituted for <code class="docutils literal notranslate"><span class="pre">%s</span></code>; if the part does not contain <code class="docutils literal notranslate"><span class="pre">%s</span></code>, it is simply
  184. interpreted as the name of the browser to launch. <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
  185. <p>For non-Unix platforms, or when a remote browser is available on Unix, the
  186. controlling process will not wait for the user to finish with the browser, but
  187. allow the remote browser to maintain its own windows on the display. If remote
  188. browsers are not available on Unix, the controlling process will launch a new
  189. browser and wait.</p>
  190. <p>The script <strong class="program">webbrowser</strong> can be used as a command-line interface for the
  191. module. It accepts a URL as the argument. It accepts the following optional
  192. parameters: <code class="docutils literal notranslate"><span class="pre">-n</span></code> opens the URL in a new browser window, if possible;
  193. <code class="docutils literal notranslate"><span class="pre">-t</span></code> opens the URL in a new browser page (“tab”). The options are,
  194. naturally, mutually exclusive. Usage example:</p>
  195. <div class="highlight-python3 notranslate"><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">&quot;https://www.python.org&quot;</span>
  196. </pre></div>
  197. </div>
  198. <div class="availability docutils container">
  199. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  200. <p>This module does not work or is not available on WebAssembly platforms
  201. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  202. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  203. </div>
  204. <p>The following exception is defined:</p>
  205. <dl class="py exception">
  206. <dt class="sig sig-object py" id="webbrowser.Error">
  207. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#webbrowser.Error" title="Link to this definition">¶</a></dt>
  208. <dd><p>Exception raised when a browser control error occurs.</p>
  209. </dd></dl>
  210. <p>The following functions are defined:</p>
  211. <dl class="py function">
  212. <dt class="sig sig-object py" id="webbrowser.open">
  213. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autoraise</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open" title="Link to this definition">¶</a></dt>
  214. <dd><p>Display <em>url</em> using the default browser. If <em>new</em> is 0, the <em>url</em> is opened
  215. in the same browser window if possible. If <em>new</em> is 1, a new browser window
  216. is opened if possible. If <em>new</em> is 2, a new browser page (“tab”) is opened
  217. if possible. If <em>autoraise</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the window is raised if possible
  218. (note that under many window managers this will occur regardless of the
  219. setting of this variable).</p>
  220. <p>Note that on some platforms, trying to open a filename using this function,
  221. may work and start the operating system’s associated program. However, this
  222. is neither supported nor portable.</p>
  223. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">webbrowser.open</span></code> with argument <code class="docutils literal notranslate"><span class="pre">url</span></code>.</p>
  224. </dd></dl>
  225. <dl class="py function">
  226. <dt class="sig sig-object py" id="webbrowser.open_new">
  227. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">open_new</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open_new" title="Link to this definition">¶</a></dt>
  228. <dd><p>Open <em>url</em> in a new window of the default browser, if possible, otherwise, open
  229. <em>url</em> in the only browser window.</p>
  230. </dd></dl>
  231. <dl class="py function">
  232. <dt class="sig sig-object py" id="webbrowser.open_new_tab">
  233. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">open_new_tab</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.open_new_tab" title="Link to this definition">¶</a></dt>
  234. <dd><p>Open <em>url</em> in a new page (“tab”) of the default browser, if possible, otherwise
  235. equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_new()</span></code></a>.</p>
  236. </dd></dl>
  237. <dl class="py function">
  238. <dt class="sig sig-object py" id="webbrowser.get">
  239. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">using</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.get" title="Link to this definition">¶</a></dt>
  240. <dd><p>Return a controller object for the browser type <em>using</em>. If <em>using</em> is
  241. <code class="docutils literal notranslate"><span class="pre">None</span></code>, return a controller for a default browser appropriate to the
  242. caller’s environment.</p>
  243. </dd></dl>
  244. <dl class="py function">
  245. <dt class="sig sig-object py" id="webbrowser.register">
  246. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">constructor</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">instance</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">preferred</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.register" title="Link to this definition">¶</a></dt>
  247. <dd><p>Register the browser type <em>name</em>. Once a browser type is registered, the
  248. <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal notranslate"><span class="pre">get()</span></code></a> function can return a controller for that browser type. If
  249. <em>instance</em> is not provided, or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>constructor</em> will be called without
  250. parameters to create an instance when needed. If <em>instance</em> is provided,
  251. <em>constructor</em> will never be called, and may be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  252. <p>Setting <em>preferred</em> to <code class="docutils literal notranslate"><span class="pre">True</span></code> makes this browser a preferred result for
  253. a <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal notranslate"><span class="pre">get()</span></code></a> call with no argument. Otherwise, this entry point is only
  254. useful if you plan to either set the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">BROWSER</span></code> variable or call
  255. <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal notranslate"><span class="pre">get()</span></code></a> with a nonempty argument matching the name of a handler you
  256. declare.</p>
  257. <div class="versionchanged">
  258. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>preferred</em> keyword-only parameter was added.</p>
  259. </div>
  260. </dd></dl>
  261. <p>A number of browser types are predefined. This table gives the type names that
  262. may be passed to the <a class="reference internal" href="#webbrowser.get" title="webbrowser.get"><code class="xref py py-func docutils literal notranslate"><span class="pre">get()</span></code></a> function and the corresponding instantiations
  263. for the controller classes, all defined in this module.</p>
  264. <table class="docutils align-default">
  265. <thead>
  266. <tr class="row-odd"><th class="head"><p>Type Name</p></th>
  267. <th class="head"><p>Class Name</p></th>
  268. <th class="head"><p>Notes</p></th>
  269. </tr>
  270. </thead>
  271. <tbody>
  272. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'mozilla'</span></code></p></td>
  273. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Mozilla('mozilla')</span></code></p></td>
  274. <td></td>
  275. </tr>
  276. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'firefox'</span></code></p></td>
  277. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Mozilla('mozilla')</span></code></p></td>
  278. <td></td>
  279. </tr>
  280. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'epiphany'</span></code></p></td>
  281. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Epiphany('epiphany')</span></code></p></td>
  282. <td></td>
  283. </tr>
  284. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'kfmclient'</span></code></p></td>
  285. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Konqueror()</span></code></p></td>
  286. <td><p>(1)</p></td>
  287. </tr>
  288. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'konqueror'</span></code></p></td>
  289. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Konqueror()</span></code></p></td>
  290. <td><p>(1)</p></td>
  291. </tr>
  292. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'kfm'</span></code></p></td>
  293. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Konqueror()</span></code></p></td>
  294. <td><p>(1)</p></td>
  295. </tr>
  296. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'opera'</span></code></p></td>
  297. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Opera()</span></code></p></td>
  298. <td></td>
  299. </tr>
  300. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'links'</span></code></p></td>
  301. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericBrowser('links')</span></code></p></td>
  302. <td></td>
  303. </tr>
  304. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'elinks'</span></code></p></td>
  305. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Elinks('elinks')</span></code></p></td>
  306. <td></td>
  307. </tr>
  308. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'lynx'</span></code></p></td>
  309. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericBrowser('lynx')</span></code></p></td>
  310. <td></td>
  311. </tr>
  312. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w3m'</span></code></p></td>
  313. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericBrowser('w3m')</span></code></p></td>
  314. <td></td>
  315. </tr>
  316. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'windows-default'</span></code></p></td>
  317. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">WindowsDefault</span></code></p></td>
  318. <td><p>(2)</p></td>
  319. </tr>
  320. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'macosx'</span></code></p></td>
  321. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">MacOSXOSAScript('default')</span></code></p></td>
  322. <td><p>(3)</p></td>
  323. </tr>
  324. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'safari'</span></code></p></td>
  325. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">MacOSXOSAScript('safari')</span></code></p></td>
  326. <td><p>(3)</p></td>
  327. </tr>
  328. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'google-chrome'</span></code></p></td>
  329. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Chrome('google-chrome')</span></code></p></td>
  330. <td></td>
  331. </tr>
  332. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'chrome'</span></code></p></td>
  333. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Chrome('chrome')</span></code></p></td>
  334. <td></td>
  335. </tr>
  336. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'chromium'</span></code></p></td>
  337. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Chromium('chromium')</span></code></p></td>
  338. <td></td>
  339. </tr>
  340. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'chromium-browser'</span></code></p></td>
  341. <td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Chromium('chromium-browser')</span></code></p></td>
  342. <td></td>
  343. </tr>
  344. </tbody>
  345. </table>
  346. <p>Notes:</p>
  347. <ol class="arabic simple">
  348. <li><p>“Konqueror” is the file manager for the KDE desktop environment for Unix, and
  349. only makes sense to use if KDE is running. Some way of reliably detecting KDE
  350. would be nice; the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">KDEDIR</span></code> variable is not sufficient. Note also that
  351. the name “kfm” is used even when using the <strong class="program">konqueror</strong> command with KDE
  352. 2 — the implementation selects the best strategy for running Konqueror.</p></li>
  353. <li><p>Only on Windows platforms.</p></li>
  354. <li><p>Only on macOS platform.</p></li>
  355. </ol>
  356. <div class="versionadded">
  357. <p><span class="versionmodified added">New in version 3.3: </span>Support for Chrome/Chromium has been added.</p>
  358. </div>
  359. <div class="versionchanged">
  360. <p><span class="versionmodified changed">Changed in version 3.12: </span>Support for several obsolete browsers has been removed.
  361. Removed browsers include Grail, Mosaic, Netscape, Galeon,
  362. Skipstone, Iceape, and Firefox versions 35 and below.</p>
  363. </div>
  364. <div class="deprecated-removed">
  365. <p><span class="versionmodified">Deprecated since version 3.11, will be removed in version 3.13: </span><code class="xref py py-class docutils literal notranslate"><span class="pre">MacOSX</span></code> is deprecated, use <code class="xref py py-class docutils literal notranslate"><span class="pre">MacOSXOSAScript</span></code> instead.</p>
  366. </div>
  367. <p>Here are some simple examples:</p>
  368. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;https://docs.python.org/&#39;</span>
  369. <span class="c1"># Open URL in a new tab, if a browser window is already open.</span>
  370. <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>
  371. <span class="c1"># Open URL in new window, raising the window if possible.</span>
  372. <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>
  373. </pre></div>
  374. </div>
  375. <section id="browser-controller-objects">
  376. <span id="browser-controllers"></span><h2>Browser Controller Objects<a class="headerlink" href="#browser-controller-objects" title="Link to this heading">¶</a></h2>
  377. <p>Browser controllers provide these methods which parallel three of the
  378. module-level convenience functions:</p>
  379. <dl class="py attribute">
  380. <dt class="sig sig-object py" id="webbrowser.name">
  381. <span class="sig-prename descclassname"><span class="pre">webbrowser.</span></span><span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#webbrowser.name" title="Link to this definition">¶</a></dt>
  382. <dd><p>System-dependent name for the browser.</p>
  383. </dd></dl>
  384. <dl class="py method">
  385. <dt class="sig sig-object py" id="webbrowser.controller.open">
  386. <span class="sig-prename descclassname"><span class="pre">controller.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">new</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">autoraise</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open" title="Link to this definition">¶</a></dt>
  387. <dd><p>Display <em>url</em> using the browser handled by this controller. If <em>new</em> is 1, a new
  388. browser window is opened if possible. If <em>new</em> is 2, a new browser page (“tab”)
  389. is opened if possible.</p>
  390. </dd></dl>
  391. <dl class="py method">
  392. <dt class="sig sig-object py" id="webbrowser.controller.open_new">
  393. <span class="sig-prename descclassname"><span class="pre">controller.</span></span><span class="sig-name descname"><span class="pre">open_new</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open_new" title="Link to this definition">¶</a></dt>
  394. <dd><p>Open <em>url</em> in a new window of the browser handled by this controller, if
  395. possible, otherwise, open <em>url</em> in the only browser window. Alias
  396. <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_new()</span></code></a>.</p>
  397. </dd></dl>
  398. <dl class="py method">
  399. <dt class="sig sig-object py" id="webbrowser.controller.open_new_tab">
  400. <span class="sig-prename descclassname"><span class="pre">controller.</span></span><span class="sig-name descname"><span class="pre">open_new_tab</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">url</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#webbrowser.controller.open_new_tab" title="Link to this definition">¶</a></dt>
  401. <dd><p>Open <em>url</em> in a new page (“tab”) of the browser handled by this controller, if
  402. possible, otherwise equivalent to <a class="reference internal" href="#webbrowser.open_new" title="webbrowser.open_new"><code class="xref py py-func docutils literal notranslate"><span class="pre">open_new()</span></code></a>.</p>
  403. </dd></dl>
  404. <p class="rubric">Footnotes</p>
  405. <aside class="footnote-list brackets">
  406. <aside class="footnote brackets" id="id2" role="doc-footnote">
  407. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  408. <p>Executables named here without a full path will be searched in the
  409. directories given in the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable.</p>
  410. </aside>
  411. </aside>
  412. </section>
  413. </section>
  414. <div class="clearer"></div>
  415. </div>
  416. </div>
  417. </div>
  418. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  419. <div class="sphinxsidebarwrapper">
  420. <div>
  421. <h3><a href="../contents.html">Table of Contents</a></h3>
  422. <ul>
  423. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code> — Convenient web-browser controller</a><ul>
  424. <li><a class="reference internal" href="#browser-controller-objects">Browser Controller Objects</a></li>
  425. </ul>
  426. </li>
  427. </ul>
  428. </div>
  429. <div>
  430. <h4>Previous topic</h4>
  431. <p class="topless"><a href="internet.html"
  432. title="previous chapter">Internet Protocols and Support</a></p>
  433. </div>
  434. <div>
  435. <h4>Next topic</h4>
  436. <p class="topless"><a href="wsgiref.html"
  437. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code> — WSGI Utilities and Reference Implementation</a></p>
  438. </div>
  439. <div role="note" aria-label="source link">
  440. <h3>This Page</h3>
  441. <ul class="this-page-menu">
  442. <li><a href="../bugs.html">Report a Bug</a></li>
  443. <li>
  444. <a href="https://github.com/python/cpython/blob/main/Doc/library/webbrowser.rst"
  445. rel="nofollow">Show Source
  446. </a>
  447. </li>
  448. </ul>
  449. </div>
  450. </div>
  451. <div id="sidebarbutton" title="Collapse sidebar">
  452. <span>«</span>
  453. </div>
  454. </div>
  455. <div class="clearer"></div>
  456. </div>
  457. <div class="related" role="navigation" aria-label="related navigation">
  458. <h3>Navigation</h3>
  459. <ul>
  460. <li class="right" style="margin-right: 10px">
  461. <a href="../genindex.html" title="General Index"
  462. >index</a></li>
  463. <li class="right" >
  464. <a href="../py-modindex.html" title="Python Module Index"
  465. >modules</a> |</li>
  466. <li class="right" >
  467. <a href="wsgiref.html" title="wsgiref — WSGI Utilities and Reference Implementation"
  468. >next</a> |</li>
  469. <li class="right" >
  470. <a href="internet.html" title="Internet Protocols and Support"
  471. >previous</a> |</li>
  472. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  473. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  474. <li class="switchers">
  475. <div class="language_switcher_placeholder"></div>
  476. <div class="version_switcher_placeholder"></div>
  477. </li>
  478. <li>
  479. </li>
  480. <li id="cpython-language-and-version">
  481. <a href="../index.html">3.12.3 Documentation</a> &#187;
  482. </li>
  483. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  484. <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> &#187;</li>
  485. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code> — Convenient web-browser controller</a></li>
  486. <li class="right">
  487. <div class="inline-search" role="search">
  488. <form class="inline-search" action="../search.html" method="get">
  489. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  490. <input type="submit" value="Go" />
  491. </form>
  492. </div>
  493. |
  494. </li>
  495. <li class="right">
  496. <label class="theme-selector-label">
  497. Theme
  498. <select class="theme-selector" oninput="activateTheme(this.value)">
  499. <option value="auto" selected>Auto</option>
  500. <option value="light">Light</option>
  501. <option value="dark">Dark</option>
  502. </select>
  503. </label> |</li>
  504. </ul>
  505. </div>
  506. <div class="footer">
  507. &copy;
  508. <a href="../copyright.html">
  509. Copyright
  510. </a>
  511. 2001-2024, Python Software Foundation.
  512. <br />
  513. This page is licensed under the Python Software Foundation License Version 2.
  514. <br />
  515. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  516. <br />
  517. See <a href="/license.html">History and License</a> for more information.<br />
  518. <br />
  519. The Python Software Foundation is a non-profit corporation.
  520. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  521. <br />
  522. <br />
  523. Last updated on Apr 09, 2024 (13:47 UTC).
  524. <a href="/bugs.html">Found a bug</a>?
  525. <br />
  526. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  527. </div>
  528. </body>
  529. </html>
上海开阖软件有限公司 沪ICP备12045867号-1