• 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

    1. import os
    2. import webbrowser as web
    3. GOOGLE_CHROME_BROWSER = 'google-chrome'
    4. new_url = 'https://blog.csdn.net/qq_15711195?spm=1018.2226.3001.5343'
    5. chrome_addr = os.popen('type ' + GOOGLE_CHROME_BROWSER).readlines()[0].split()[2]
    6. web.register(GOOGLE_CHROME_BROWSER, None, web.BackgroundBrowser(chrome_addr))
    7. browser = web.get(GOOGLE_CHROME_BROWSER)
    8. #打开一个新的页面
    9. browser.open_new_tab(new_url)

  • 相关阅读:
    pytest fixture 高级使用
    SpringCloud中服务熔断组件Hystrix和网关组件Gateway的使用
    搜维尔科技:特斯拉称工厂内有两台人形机器人开始自主工作
    设计模式之策略模式
    机械硬盘HDD
    网站TDK采集工具-网站的TDK设置方法
    2023数维杯数学建模竞赛D题思路+模型+代码+论文
    回应:淘宝支持使用微信支付?
    [正确重装docker] Win10 重装 Docker 提示 Exising installation is up to date 的正确姿势
    产品经理常用的工具有哪些?
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/125457213