iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python基础--webbrowser
  • 227
分享到

Python基础--webbrowser

基础Pythonwebbrowser 2023-01-31 06:01:11 227人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

很多人,一提到python,想到的就是爬虫。我会一步一步的教你如何爬出某个网站。今天就先介绍一下WEBbrowser,这个词您肯定不会陌生。对,就是浏览器。看看Python中对webbrowser的描述:The webbrowser mo

很多人,一提到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_new().

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

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.


上面的都是官方的英文描述,单词都很简单,如果看不懂,劝你还是别编程了。


下面是几个应用实例:

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


--结束END--

本文标题: Python基础--webbrowser

本文链接: https://www.lsjlt.com/news/190040.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python基础--webbrowser
    很多人,一提到Python,想到的就是爬虫。我会一步一步的教你如何爬出某个网站。今天就先介绍一下webbrowser,这个词您肯定不会陌生。对,就是浏览器。看看Python中对webbrowser的描述:The webbrowser mo...
    99+
    2023-01-31
    基础 Python webbrowser
  • Python基础——1基础
    输出 print(‘把子肉爱上热干面’,‘哈哈’)  # ‘,’输出为空格 输人 name = input(‘提示的内容’) /浮点除法  %.6f //地板除法  整除 %  取余 python编码问题 (采用Unicode编码) ...
    99+
    2023-01-30
    基础 Python
  • Python基础-Python基础使用
    上篇文章 Python基础-初识Python 我们已经知道了什么是Python,Python的用处、和Python的解释器、Python的安装,这篇文章,我们主要讲Python的使用入门本文防盗链:http://python789.blog...
    99+
    2023-01-31
    基础 Python
  • Python基础篇-Python基础语法
    为什么学习pythonhttp://www.apelearn.com/bbs/thread-7739-1-1.html Python的安装 getconf LONG_BIT     查看系统版本多少位 rpm -q python uname...
    99+
    2023-01-31
    基础 语法 Python
  • Python基础--Python3基础语
    Python3 基础语法编码默认情况下,Python3源码文件以UTF-8编码,所有字符串都是Unicode字符串。当然也可以为源码文件指定不同的编码,例如:# -*- coding: cp-1252 -*-标识符1.第一个字符必须是字母表...
    99+
    2023-01-31
    基础 Python
  • Python基础
    主要是复习时总结自己不太熟悉的知识点了(面向Internet的总结)。 函数的参数 位置参数——按位置依次对应 关键字参数——按“键值对”对应 func('hello', val = 1) 调用时:若有位置参数,位置参数必须在关键字参...
    99+
    2023-01-30
    基础 Python
  • python 基础
    #列表是python最常用的数据类型,它可以作为一个方括号内的逗号分隔值出现 列表的数据类型不需要相同的类型 创建一个列表,只有在方括号([])以逗号(,)分割开即可,不需要相同的数据类型 列表表示方式 list1=['gao_wang',...
    99+
    2023-01-31
    基础 python
  • 【python基础】——python 复
    复数可以用使用函数 complex(real, imag) 或者是带有后缀j的浮点数来指定。比如: >>> a = complex(2, 4) >>> b = 3 - 5j >>>...
    99+
    2023-01-31
    基础 python
  • 《Python入门到精通》webbrowser模块详解,Python webbrowser标准库,Python浏览器控制工具
    「作者主页」:士别三日wyx 「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」:小白零基础《Python入门到精通》 webbro...
    99+
    2023-09-01
    python 机器学习 人工智能 安全
  • python 基础 day3
    python 操作文件的常用方式有如下 读文件:r 模式 实例演示1:f1 = open(file='D:\Python3.5-learn\模块2\character3_文件操作\staff_table.txt',mode='r',enco...
    99+
    2023-01-31
    基础 python
  • day01 python基础
    # 1.python简介# 解释型 弱类型 高级编程语言# 2.安装# 3.第一个程序# helloworld # 4.变量# 程序执行过程中的中间值,储存数据,用于后面的程序进行调用# 5.变量的命名规范(重点) ...
    99+
    2023-01-30
    基础 python
  • Python-基础-day4
     深浅copy                          1、先看赋值运算 h1 = [1,2,3,['aihuidi','hhhh']] h2 = h1 h1[0] = 111 print(h1) print(h2) #结果:...
    99+
    2023-01-30
    基础 Python
  • day09-python基础
    一、Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二、Python开发  a.开发     1.开发语言       高级语言:Python Java、PHP C# Go ruby C++... ===》 字...
    99+
    2023-01-31
    基础 python
  • python基础1
    python在windows安装先下载python 最新版本 3.5.1 或2.7.11运行安装程序。修改环境变量计算机-属性-高级系统设置-环境变量-path 将python安装路径填写到环境变量中。与上一个变量用";"分号分割如果同时安...
    99+
    2023-01-31
    基础 python
  • Python基础(中)
    前言 print(" _ooOoo_ ") print(" o8888888o ...
    99+
    2023-01-30
    基础 Python
  • python基础(一)
    1.计算机是由什么组成的 CPU、内存、硬盘、输入输出设备 CPU 处理各种数据 相当于人的大脑 内存 存储临时数据 相当于人的临时记忆 硬盘 存储数据 相当于人的...
    99+
    2023-01-30
    基础 python
  • Python-基础-day2
    Python环境的安装                                                                               安装Python: windows:           ...
    99+
    2023-01-30
    基础 Python
  • Day3 python基础
    集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集、差集、并集等关系 list_1 = [1, 4, 5, 7, 3, 6, 7, 9] list_1 = ...
    99+
    2023-01-31
    基础 python
  • Python基础(二)
    内置函数文件操作操作文件时,一般需要经历如下步骤:打开文件操作文件一、打开文件1文件句柄 = file('文件路径', '模式')注:python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调...
    99+
    2023-01-31
    基础 Python
  • Python-基础-day1
    一、python的介绍                                                            1、python的出现与应用场景                  python的创始人为吉多·...
    99+
    2023-01-30
    基础 Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作