Python基础--webbrowser
2016-03-27 15:01
549 查看
很多人,一提到Python,想到的就是爬虫。我会一步一步的教你如何爬出某个网站。
今天就先介绍一下webbrowser,这个词您肯定不会陌生。对,就是浏览器。
看看Python中对webbrowser的描述:
The
下面就是对webbrowser的简单实用了:首先当然是导入webbrowser模块了:import webbrowser
但是这个时候等等,我有话要说。在C++中,如果一个变量的名称太长,我们往往实用typedef进行缩写。Python中,同样可以,比如我们嫌webbrowser太长了,希望用web替代,则可以这么导入:
import webbrowser as web
接下来就介绍一些函数了:
上面的都是官方的英文描述,单词都很简单,如果看不懂,劝你还是别编程了。
下面是几个应用实例:1用指定的浏览器来加载url
2对比应用import webbrowser
url = '
关闭浏览器
对了,忘了写如何关闭浏览器了
执行命令行即可:
import os
os.system('taskkill /F /IM chrome.exe')
今天就先介绍一下webbrowser,这个词您肯定不会陌生。对,就是浏览器。
看看Python中对webbrowser的描述:
The
webbrowsermodule 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.
下面就是对webbrowser的简单实用了:首先当然是导入webbrowser模块了:import webbrowser
但是这个时候等等,我有话要说。在C++中,如果一个变量的名称太长,我们往往实用typedef进行缩写。Python中,同样可以,比如我们嫌webbrowser太长了,希望用web替代,则可以这么导入:
import webbrowser as web
接下来就介绍一些函数了:
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
open_new().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
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.
上面的都是官方的英文描述,单词都很简单,如果看不懂,劝你还是别编程了。
下面是几个应用实例:1用指定的浏览器来加载url
import webbrowser b = webbrowser.get('chrome') b.open('http://blog.csdn.net/wangshubo1989')
2对比应用import webbrowser
url = '
http://blog.csdn.net/wangshubo1989'# 默认浏览器打开webbrowser.open_new(url) # opens in default browser# 使用 safari 打开webbrowser.get('safari').open_new(url)# 在浏览器中用新标签打开webbrowser.open_new_tab(url) # opens in default browser# 在Safari中新建标签并打开urlwebbrowser.get('safari').open_new_tab(url)
关闭浏览器
对了,忘了写如何关闭浏览器了
执行命令行即可:
import os
os.system('taskkill /F /IM chrome.exe')
相关文章推荐
- Python基础--webbrowser
- 一个小脚本遇到的问题之python查看网站编码
- Python2.7写入中文数据进sqlite3报错
- python去掉字符串中空格的方法
- pythond对象、异常、反射的学习笔记
- ubuntu下同时存在Python2.7.6和Python3时更改优先级
- 笨方法学Python之一(原第六)
- python多版本控制管理工具:Pyenv
- Python变量基础知识
- 【Python数据分析】Python模拟登录(一) requests.Session应用
- python3.X并发socketserver模块
- python操作Excel读写(使用xlrd和xlrt)
- python连接mysql
- Python lamda
- Logistic回归模型和Python实现
- [python]“静态”变量:谈类属性和实例属性
- 自动化打包资源混淆集成python实践----打包
- [python]python学习线路
- numpy返回array中元素的index
- python学习之day9