代码之家  ›  专栏  ›  技术社区  ›  Quanti Monati

如何使用python获取默认浏览器名称?

  •  1
  • Quanti Monati  · 技术社区  · 6 年前

    以下解决方案(实际上只有一个)对我不起作用:

    How to get a name of default browser using python


    How to get name of the default browser in windows using python?

    解决办法是:

    from _winreg import HKEY_CURRENT_USER, OpenKey, QueryValue
    # In Py3, this module is called winreg without the underscore
    
    with OpenKey(HKEY_CURRENT_USER,
                 r"Software\Classes\http\shell\open\command") as key:
        cmd = QueryValue(key, None)
    

    请看一看,我的注册表实际上包含: enter image description here

    2 回复  |  直到 6 年前
        1
  •  2
  •   cody    6 年前

    以下内容在Windows 10 pro上适用:

    from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx
    
    reg_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'
    
    with OpenKey(HKEY_CURRENT_USER, reg_path) as key:
        print(QueryValueEx(key, 'ProgId'))
    

    $ python test.py
    ('ChromeHTML', 1)
    
    $ python test.py
    ('IE.HTTPS', 1)
    
        2
  •  1
  •   vikas singhal    6 年前

    HKEY|U CURRENT|U USER\SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations(http | https)\UserChoice

        3
  •  0
  •   Conrad B    5 年前
    def get_windows_default_browser_launch():
        """ On windows, return the default browser for 'https' urls
        returns: example '"C:\Program Files\Mozilla Firefox\firefox.exe" -osint -url "%1"'
        """
        import winreg
        key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), r"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice")
        prog_id, _ = winreg.QueryValueEx(key, "ProgId")
        key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE), r"SOFTWARE\Classes\{}\shell\open\command".format(prog_id))
        launch_string, _ = winreg.QueryValueEx(key, "")  # read the default value
        return launch_string
    

    Windows 10 Python3可能希望更改“http”而不是https的密钥,但这是我的代码,因为我的上下文是安全服务器的。我想要浏览器的二进制名称和路径,这只是多一行。