webbrowser
互联网协议和支持
源代码: 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 from this module will do the right thing.
open()
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.
若环境变量 BROWSER exists, it is interpreted as the os.pathsep -separated list of browsers to try ahead of the platform 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 ]
BROWSER
os.pathsep
%s
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.
脚本 webbrowser can be used as a command-line interface for the module. It accepts a URL as the argument. It accepts the following optional 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, naturally, mutually exclusive. Usage example:
-n
-t
python -m webbrowser -t "https://www.python.org"
可用性 :非 Emscripten,非 WASI。
本模块不工作 (或不可用) 于 WebAssembly 平台 wasm32-emscripten and wasm32-wasi 。见 WebAssembly 平台 了解更多信息。
wasm32-emscripten
wasm32-wasi
定义了以下异常:
异常被引发当发生浏览器控件错误时。
定义了下列函数:
显示 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).
True
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.
引发 审计事件 webbrowser.open 采用自变量 url .
webbrowser.open
url
打开 url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
打开 url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new() .
open_new()
返回控制器对象对于浏览器类型 使用 。若 使用 is None , return a controller for a default browser appropriate to the caller’s environment.
None
注册浏览器类型 name . Once a browser type is registered, the get() function can return a controller for that browser type. If instance is not provided, or is None , 构造函数 will be called without parameters to create an instance when needed. If instance is provided, 构造函数 will never be called, and may be None .
get()
设置 preferred to True makes this browser a preferred result for a get() call with no argument. Otherwise, this entry point is only useful if you plan to either set the BROWSER variable or call get() with a nonempty argument matching the name of a handler you declare.
3.7 版改变: preferred keyword-only parameter was added.
A number of browser types are predefined. This table gives the type names that may be passed to the get() function and the corresponding instantiations for the controller classes, all defined in this module.
类型名称
类名
注意事项
'mozilla'
Mozilla('mozilla')
'firefox'
'epiphany'
Epiphany('epiphany')
'kfmclient'
Konqueror()
'konqueror'
'kfm'
'opera'
Opera()
'links'
GenericBrowser('links')
'elinks'
Elinks('elinks')
'lynx'
GenericBrowser('lynx')
'w3m'
GenericBrowser('w3m')
'windows-default'
WindowsDefault
'macosx'
MacOSXOSAScript('default')
'safari'
MacOSXOSAScript('safari')
'google-chrome'
Chrome('google-chrome')
'chrome'
Chrome('chrome')
'chromium'
Chromium('chromium')
'chromium-browser'
Chromium('chromium-browser')
注意事项:
“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 KDEDIR variable is not sufficient. Note also that the name “kfm” is used even when using the konqueror command with KDE 2 — the implementation selects the best strategy for running Konqueror.
KDEDIR
仅在 Windows 平台。
Only on macOS platform.
Added in version 3.3: 添加对 Chrome/Chromium 的支持。
Changed in version 3.12: Support for several obsolete browsers has been removed. Removed browsers include Grail, Mosaic, Netscape, Galeon, Skipstone, Iceape, and Firefox versions 35 and below.
Deprecated since version 3.11, will be removed in version 3.13: MacOSX is deprecated, use MacOSXOSAScript 代替。
MacOSX
MacOSXOSAScript
这里是一些简单范例:
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)
Browser controllers provide these methods which parallel three of the module-level convenience functions:
System-dependent name for the browser.
显示 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.
打开 url in a new window of the browser handled by this controller, if possible, otherwise, open url in the only browser window. Alias open_new() .
打开 url in a new page (“tab”) of the browser handled by this controller, if possible, otherwise equivalent to open_new() .
脚注
wsgiref — WSGI 实用程序和参考实现
wsgiref
键入搜索术语或模块、类、函数名称。