分类: Python/Ruby
2013-02-24 22:28:55
在python中,webbrowser这个模块提供了一个抽象的接口,用来展现基于web的文档给客户。简单的说,他会去调用你机子默认的浏览器,展示你传进去的url。当调用浏览器失败的时候,会触发webbrowser.Error的错误。模块定义的方法如下:
Display 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).
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: new can now be 2.
webbrowser.open_new(url)Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.
webbrowser.open_new_tab(url)Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to .
New in version 2.5.
webbrowser.get([name])Return a controller object for the browser type name. If name is empty, return a controller for a default browser appropriate to the caller’s environment.
webbrowser.register(name, constructor[, instance])Register the browser type name. Once a browser type is registered, the function can return a controller for that browser type. If instance is not provided, or is None, constructor will be called without parameters to create an instance when needed. If instance is provided, constructor will never be called, and may be None.
This entry point is only useful if you plan to either set the BROWSER variable or call with a nonempty argument matching the name of a handler you declare.
示例:
url = ''
# 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的文本。