非常多人,一提到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. http链接中请求进行编码,Http请求

    如果参数中含有特殊字符&,则强制URL编码<br> http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号 ...

  2. CAD二次开发控件,dwg控件,网页DWG控件,手机浏览编辑DWG控件

    梦想绘图插件5.2(MxDraw5.2) 是国内最强,最专业的CAD开发插件(控件),不需要AutoCAD就能独立运行. 控件使用VC 2010开发,具有30万行代码规模,最早从2007年第一个版本完 ...

  3. Tensorflow学习笔记(1):tf.slice()函数使用

    tensorflow 当中的一个常用函数:Slice() def slice(input_, begin, size, name=None) 函数的功能是根据begin和size指定获取input的部 ...

  4. UVA - 12589 Learning Vector(dp-01背包)

    题目: 思路: dp[j][h]表示选取了j个向量,且高度为h,利用01背包来解决问题. 没选当前的向量:dp[j][h] = dp[j][h]; 选了当前的向量:dp[j][h] = dp[j-1] ...

  5. Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)

    题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...

  6. [bzoj3671][Noi2014][随机数生成器] (贪心+位运算+卡空间)

    Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个整数 N,M,Q ,表示小H希望生成一个1到 N×M ...

  7. IIS301重定向:将不带www的域名跳转到带www上

    首先你的域名有这两条解析记录 进入服务器IIS,添加2个站点,如下图 第一个正常绑定你的域名:www.baidu.com 第二个绑定不带www的域名:baidu.com 然后点开ncgd-no-www ...

  8. fzu2143 Board Game

    Board Game Accept: 54    Submit: 151Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Descri ...

  9. windows server 2008R2 上安装配置freesshd

    从FREESSHD官方网站下载最新的软件版本,下载地址是http://www.freesshd.com/?ctt=download 双击刚刚下载的freeSSHd.exe进行安装,安装时其他都是默认安 ...

  10. java多线程编程核心技术(四)--Lock的使用

    1.jdk1.5中新增了ReentrantLock类,该类也可以实现synchronized线程之间同步互斥的效果. 2.jdk1.5中新增了Condition类.在Lock对象中可以创建多个Cond ...