python的浏览器启动IE,而不是在Windows 7默认

我试图在默认浏览器中从python启动一个本地html文件。 现在我的默认是谷歌浏览器。 如果我双击一个.html文件,chrome启动。

当我使用Python的webbrowser.open,IE浏览器启动,而不是一个空白的地址栏。

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import webbrowser >>> filename = 'test.html' >>> webbrowser.open('file://'+filename) True >>> print(webbrowser.get().__class__.__name__) WindowsDefault 

我检查了我的默认程序,他们看起来是正确的。 我在Win 7 SP1上。 为什么铬不启动?

更新 :代码将在未知的操作系统和机器上运行,所以注册浏览器或path更新不是选项。 我在考虑parsingfile://的url,然后做一个os.path.exists检查和os.path.realpath可能是答案。

我的主要问题是通过尝试prepend file://到一个相对path来看一个错误的URL。 这可以用这个来解决:

 webbrowser.open('file://' + os.path.realpath(filename)) 

使用webbrowser.open将尝试多种方法,直到一个“成功”,这是一个松散的定义。

WindowsDefault类调用os.startfile() ,失败并返回False 。 我可以通过在Windows运行命令中inputURL并查看错误消息而不是浏览器来validation。

GenericBrowserBackgroundBrowser都会调用subprocess.Popen() ,这个exe会成功,即使URL不True ,也会返回True 。 IE浏览器没有提供这个问题的迹象,所有其他的浏览器有一个很好的消息,说他们无法find该文件。

  1. GenericBrowser由环境variablesBROWSER设置,并且是第一个。
  2. WindowsDefault是第二个。
  3. BackgroundBrowser是最后一个,如果没有其他的工作,包括回退IE浏览器。

这是我的原始设置:

 >>> import webbrowser >>> webbrowser._tryorder ['windows-default', 'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE'] >>> webbrowser._browsers.items() [('windows-default', [<class 'webbrowser.WindowsDefault'>, None]), ('c:\\program files\\internet explorer\\iexplore.exe', [None, <webbrowser.BackgroundBrowser object at 0x00000000022E3898>])] >>> 

这是我修改环境variables后的设置:

 C:>path=C:\Program Files (x86)\Mozilla Firefox;%path% C:>set BROWSER=C:\Users\Scott\AppData\Local\Google\Chrome\Application\chrome.exe C:>python Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import webbrowser >>> webbrowser._tryorder ['C:\\Users\\Scott\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe', 'windows-default', 'firefox', 'C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE'] >>> webbrowser._browsers.items() [('windows-default', [<class 'webbrowser.WindowsDefault'>, None]), ('c:\\program files\\internet explorer\\iexplore.exe',[None, <webbrowser.BackgroundBrowser object at 0x000000000235E828>]), ('firefox', [None, <webbrowser.BackgroundBrowser object at 0x000000000235E780>]), ('c:\\users\\scott\\appdata\\local\\google\\chrome\\application\\chrome.exe', [None, <webbrowser.GenericBrowser object at 0x000000000235E8D0>])] >>> 

webbrowser._tryorder给出了浏览器的尝试列表。 注册chrome或者添加一个BROWSER env var或者修改我的path都可以让我得到一个更好的错误信息。

谢谢你们的帮助,如果没有你们的想法,我不可能解决这个问题。

您可以使用get(name)来使用特定的浏览器。

您需要注册Chrome浏览器 ,因为它似乎不是预定义的浏览器types之一 ,那么您应该可以这样做:

webbrowser.get('chrome').open('http://www.google.com')

更新:

其实,你可能只能做到以下一种:

webbrowser.get('windows-default').open('http://www.google.com') webbrowser.get('macosx').open('http://www.google.com')

文档显示没有预定义的Linux默认值。

这为我打开了一个新的Chrome标签,它仍然是独立于操作系统:

 webbrowser.get().open('http://www.google.com') 

奇怪的是,没有get()调用,它仍然使用IE。 这看起来像一个简单的解决方法的错误。

webbrowser模块应该使用默认的浏览器,所以这可能是一个错误。 另一方面,使用文档中的解释来解决您的问题:

如果环境variablesBROWSER存在,它将被解释为覆盖浏览器的平台默认列表,作为按顺序尝试的os.pathsep分隔的浏览器列表。 当列表部分的值包含string%s时,则将其解释为文字浏览器命令行,用于replace%s的参数URL; 如果该部分不包含%s,则将其简单解释为要启动的浏览器的名称。

查看模块源代码 ,它首先尝试使用Windows默认浏览器,但如果它不起作用,它会search常见的浏览器名称,即命令,即。 在PATHvariables中。 尝试将您的Web浏览器的位置添加到您的PATH。

我有与Firefox相同的问题。 在ff中打开http://www.google.com ,而在IE中打开file:///test.html。

网页浏览器的医生说:

请注意,在某些平台上,尝试使用此function打开文件名可能会起作用并启动操作系统的关联程序。 但是,这既不支持也不便携。

这个问题在我看来只适用于file:///协议URL,可能没有注册到chrome,所以os.startfile()(这是webbrowser.open在Windows上尝试的第一件事)在Internet Explorer中打开它们。 我不认为把你的其他浏览器放在PATH中会有帮助,因为甚至在尝试path中的浏览器之前os.startfile()仍然被调用。

可以做的是在registry中检查http://的默认浏览器(例如,通过检查registry项HKEY_CLASSES_ROOT\http\shell\open\command )并使用file:/// URLs。 不漂亮,但它应该工作。

 import _winreg import webbrowser import shlex import subprocess def win_browser_open(url): if url.startswith('file:///'): browser = _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT, r'http\shell\open\command') browser = browser.replace('%1', url) subprocess.Popen(shlex.split(browser)) else: webbrowser.open(url) 

使用Windows 10,简而言之,所有不包含https://example.com格式完整URL的内容都是在IE中为我打开的。 例如,如果我说

 webbrowser.open("https://www.example.com") 

它会在Chrome中打开一个新的标签,而

 webbrowser.open("example.com") 

会打开IE。 任何.get()将导致它根本不打开浏览器。

有点奇怪的行为,但我可以看到,这是一个复杂的事情做实施,可能操作系统是这个行为的责任。