python webbrowser安装_python模块webbrowser

段干开宇
2023-12-01

在python中,webbrowser这个模块提供了一个抽象的接口,用来展现基于web的文档给客户。简单的说,他会去调用你机子默认的浏览器,展示你传进去的url。当调用浏览器失败的时候,会触发webbrowser.Error的错误。模块定义的方法如下:

webbrowser.open(url[,new=0[,autoraise=True]])

Displayurlusing the default browser. Ifnewis 0, theurlis opened in the same browser window if possible. Ifnewis 1, a new browser window is opened if possible. Ifnewis 2, a new

browser page (“tab”) is opened if possible. IfautoraiseisTrue, the window is

raised if possible (note that under many window managers this will occur

regardless of the setting of this variable).

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.

Changed in version 2.5:newcan now be 2.

webbrowser.open_new(url)

Openurlin a new window of the default browser, if possible,

otherwise, openurlin the only browser window.

webbrowser.open_new_tab(url)

Openurlin a new page (“tab”) of the default browser, if possible,

otherwise equivalent toopen_new().

New in version

2.5.

webbrowser.get([name])

Return a controller object for the browser typename. Ifnameis empty, return a controller for a default browser appropriate to

the caller’s environment.

webbrowser.register(name,constructor[,instance])

Register the browser typename. Once a browser type is registered,

theget()function can return a controller for that

browser type. Ifinstanceis not provided, or isNone,constructorwill be called without parameters to create an instance

when needed. Ifinstanceis provided,constructorwill never

be called, and may beNone.

This entry point is only useful if you plan to either set theBROWSERvariable or callget()with a nonempty argument matching the name of a

handler you declare.

示例:

url = 'http://www.python.org/'

# Open URL in a new tab, if a browser window is already open.

webbrowser.open_new_tab(url + 'doc/')

# Open URL in new window, raising the window if possible.

webbrowser.open_new(url)

通过简单的几句,就可以调用本地浏览器展示基于web的文本。

 类似资料: