Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1134874
  • 博文数量: 103
  • 博客积分: 1897
  • 博客等级: 上尉
  • 技术积分: 1717
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-19 21:02
文章分类

全部博文(103)

文章存档

2013年(19)

2012年(84)

分类: Python/Ruby

2013-02-24 22:28:55

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



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

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的文本。



		




阅读(12146) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~