非常多人,一提到Python,想到的就是爬虫。我会一步一步的教你怎样爬出某个站点。

今天就先介绍一下webbrowser,这个词您肯定不会陌生。对,就是浏览器。

看看Python中对webbrowser的描写叙述:

The webbrowser module 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(urlnew=0autoraise=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(nameconstructor[, 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 Noneconstructor will be called without parameters to create an instance when needed. If instance is provided, constructor will never be called, and may be None.

上面的都是官方的英文描写叙述,单词都非常easy。假设看不懂,劝你还是别编程了。

以下是几个应用实例:

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')

最新文章

  1. Debug Databinding Issues in WPF
  2. shiro的使用2 灵活使用shiro的密码服务模块
  3. css3 transition属性
  4. C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
  5. 6个强大的AngularJS扩展应用
  6. 移除了css框架,世界干净了
  7. [INS-41112] Specified network interface doesnt maintain connectivi
  8. nodejs的mysql模块学习(七)连接池事件
  9. javascript dom编程艺术笔记之图片库的改进
  10. c# hasvalue属性
  11. 注册UBER(优步)司机常见问题,如何注册uber(优步)司机
  12. HDOJ2000-ASCII码排序
  13. css 小坑
  14. Django--视图函数views
  15. Sql Server 判断字符串是否可以转数字
  16. dubbo 负载均衡
  17. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem A. A + B
  18. NOI 08 石头剪刀布
  19. gcc g++支持C++11 标准编译及其区别
  20. VB 中定义FileSystemObject对象,要先添加对象

热门文章

  1. URI URL URN的区别
  2. 联想 S5【K520】免解锁BL 免rec 保留数据 Magisk Xposed 救砖 ROOT ZUI 3.7.490
  3. text-shadow的用法详解
  4. sp_Msforeachtable与sp_Msforeachdb详解
  5. Vue指令的概念
  6. JAVA程序员面试笔试宝典1
  7. gym101343J. Husam and the Broken Present 2 (状压DP)
  8. Ansible基于playbook批量修改主机名实战
  9. java基础学习日志--String、StringBuffer方法案例
  10. python re模块与正则