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

时间:2018-12-26 00:26:50

标签: python python-3.x browser windows-10

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

  

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)

但是不幸的是,在Windows 10 Pro中,我没有目标注册表值。我试图在Regedit中找到其他键,但是没有运气。

请查看我的注册表实际上包含的内容: enter image description here

3 个答案:

答案 0 :(得分:1)

请检查Windows 10中的密钥

  

HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Windows \ Shell \ Associations \ URLAssociations(http | https)\ UserChoice

答案 1 :(得分:1)

以下内容适用于Windows 10专业版:

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

结果(首先将Chrome设置为默认值,然后使用IE):

$ python test.py
('ChromeHTML', 1)

$ python test.py
('IE.HTTPS', 1)

答案 2 :(得分:0)

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的密钥,但这是我的代码,因为我的上下文是受保护的服务器。我想要浏览器的二进制名称和路径,仅此一行而已。