栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python网页信息操作——webbrowser

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.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.

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().

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.
 

二、代码示例

系统环境:Ubuntu20.04

import os
import webbrowser as web

GOOGLE_CHROME_BROWSER = 'google-chrome'

new_url = 'https://blog.csdn.net/qq_15711195?spm=1018.2226.3001.5343'
chrome_addr = os.popen('type ' + GOOGLE_CHROME_BROWSER).readlines()[0].split()[2]
web.register(GOOGLE_CHROME_BROWSER, None, web.BackgroundBrowser(chrome_addr))
browser = web.get(GOOGLE_CHROME_BROWSER)

#打开一个新的页面
browser.open_new_tab(new_url)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号