非常多人,一提到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')

Python基础--webbrowser的更多相关文章

  1. python基础系列教程——Python3.x标准模块库目录

    python基础系列教程——Python3.x标准模块库目录 文本 string:通用字符串操作 re:正则表达式操作 difflib:差异计算工具 textwrap:文本填充 unicodedata ...

  2. python基础31[常用模块介绍]

    python基础31[常用模块介绍]   python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...

  3. python基础全部知识点整理,超级全(20万字+)

    目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...

  4. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  5. Python开发【第二篇】:Python基础知识

    Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...

  6. Python小白的发展之路之Python基础(一)

    Python基础部分1: 1.Python简介 2.Python 2 or 3,两者的主要区别 3.Python解释器 4.安装Python 5.第一个Python程序 Hello World 6.P ...

  7. Python之路3【第一篇】Python基础

    本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...

  8. 进击的Python【第三章】:Python基础(三)

    Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...

  9. 进击的Python【第二章】:Python基础(二)

    Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...

随机推荐

  1. python自动化--语言基础四模块、文件读写、异常

    模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...

  2. SQL SERVER 执行计划各字段注释

    SET SHOWPLAN_ALL使 Microsoft® SQL Server™ 不执行 Transact-SQL 语句.相反,SQL Server 返回有关语句执行方式和语句预计所需资源的详细信息. ...

  3. sublime text3 =个人插件

    1.sublime text3汉化插件安装. ctrl+shift+p → Package Control:Install Package → ChineseLocalization preferen ...

  4. 解决移动端 手机号input 属性为 number,maxlength无效情况

    <input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...

  5. 14mysql事务、数据库连接池、Tomcat

    14mysql事务.数据库连接池.Tomcat-2018/07/26 1.mysql事务 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. MySQL手动控制事务 开启事 ...

  6. python 使用time / datetime进行时间、时间戳、日期转换

    python 使用time 进行时间.时间戳.日期格式转换 #!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2017/11/7 15:53 # ...

  7. Python反射、异常处理

    反射 :字符串到对象属性的映射 hasattr(obj,string), 判断对象obj里面是否有叫string的字段或方法 getattr(obj,string) 获取obj对象里名叫string的 ...

  8. circumferential averaged streamwise velocity in ParaView

    Goal: get a averaged axial velocity in a circular loop (dashed line in the following figure) Steps: ...

  9. STM32F103移值FreeRtos笔记

    RTOS版本:FreeRTOS_V8.2.2 一.下载FreeRTOS源文件       这个可以在百度上下载,或者在官网上面下载http://www.freertos.org/a00104.html ...

  10. 洛谷 4768 LOJ 2718「NOI2018」归程

    [题解] 本题有多种做法,例如可持久化并查集.kruskal重构树等. kruskal重构树的做法是这样的:先把边按照海拔h从大到小的顺序排序,然后跑kruskal建立海拔的最大生成树,顺便建krus ...