python webbrowser
来源:互联网 发布:腾讯离职 知乎 编辑:程序博客网 时间:2024/06/06 07:38
webbrowser
webbrowser一共有5个方法:
import webbrowserwebbrowser.open(url, new=0, autoraise=True)webbrowser.open_new(url)webbrowser.open_new_tab(url)webbrowser.get(using=None)webbrowser.register(name, constructor, instance=None)
首先来看python3.6.1的官方文档关于webbrowser的说明:
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.
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 open_new().
webbrowser.get(using=None)# Return a controller object for the browser type using. # If using is None, return a controller for a default browser appropriate to the caller’s environment.
webbrowser.register(name, constructor, instance=None)# Register the browser type 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, 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 get() with a nonempty argument matching the name of a handler you declare.
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.
备注:(1)“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.
(2)仅用于WIndows系统
(3)仅用于Mac OS X系统
使用默认浏览器打开URL:
import webbrowserurl = 'www.baidu.com'webbrowser.open(url)
关闭浏览器:
import osos.system('taskkill /F /IM MicrosoftEdge.exe')浏览器的名字可以从任务管理器中获取。
经测在windows(XP-32位,10-64位)系统下,open(url), open_new(url), open_new_tab(url)都是在新标签下打开网页,open()的new和autoraise参数都不起作用。
可能是因为没有BROWSER这个环境变量,使用上面表格的浏览器类注册其他浏览器后,用get()方法并不能获取对应浏览器的对象。要使用其他浏览器可以使用以下方法:
import webbrowserurl = 'www.baidu.com'chromePath = r'C:\Users\WJY\AppData\Local\Google\Chrome\Application\chrome.exe'webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath))b = webbrowser.get('chrome')b.open(url)
- python webbrowser
- python:webbrowser
- Python基础--webbrowser
- python | 打开浏览器webbrowser
- Python webbrowser模块的使用
- selenium+webdriver(python)+webbrowser入门
- WebBrowser
- WebBrowser
- webBrowser
- webBrowser
- WebBrowser
- WebBrowser
- webbrowser
- WebBrowser
- WebBrowser
- WebBrowser
- webbrowser
- WebBrowser
- C++使用vector.erase()报错:vector iterators incompatible 解决方案
- spark: RDD与DataFrame之间的相互转换
- Android 6.0权限处理
- android应用开发常用的一些协议学习
- spark学习一
- python webbrowser
- (三)jQuery.ajxa——dataType:json(2)
- Activity窗口模式
- Linux screen 命令详解
- django发送邮件
- 关于网络唤醒设置的说明
- Universal Link|iOS开发者不得不知的技术
- C# 如何实现带消息数的程序桌面图标
- git本地文件上传到远程仓库时,本地项目与远成仓库不是同一个项目的BUG