问题描述
我试图在默认浏览器从Python启动本地HTML文件。现在我的默认值是谷歌浏览器。如果我在一个.html文件双击,铬启动。
I'm attempting to launch a local html file from python in the default browser. Right now my default is google chrome. If I double-click on a .html file, chrome launches.
当我使用Python的webbrowser.open,IE启动而不是,一个空白的地址栏。
When I use python's webbrowser.open, IE launches instead, with a blank address bar.
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。为什么不铬启动?
I've checked my default programs and they look correct. I'm on Win 7 SP1. Why is chrome not launching?
更新:在code将在未知的操作系统和机器上运行,因此注册的浏览器或路径更新不选择。我在想,对于解析文件的URL://
,然后做一个 os.path.exists
检查并 os.path.realpath
可能是答案。
Update: The code will be running on unknown os's and machines, so registering browsers or path updates are not options. I'm thinking that parsing the url for file://
and then doing an os.path.exists
check and os.path.realpath
might be the answer.
推荐答案
我的主要问题是一个糟糕的URL通过尝试prePEND 文件://
来相对路径。它可以固定与此
My main issue was a bad URL by attempting prepend file://
to a relative path. It can be fixed with this:
webbrowser.open('file://' + os.path.realpath(filename))
使用 webbrowser.open
将尝试多种方法,直到一个成功,这是一个松散的定义。
Using webbrowser.open
will try multiple methods until one "succeeds", which is a loose definition.
的 WindowsDefault
调用类 os.startfile()
这将失败并返回假
。我可以进入Windows运行命令的网址,看到一个错误信息,而不是浏览器验证。
The WindowsDefault
class calls os.startfile()
which fails and returns False
. I can verify that by entering the URL in the windows run command and seeing an error message rather than a browser.
两者 GenericBrowser
和 BackgroundBrowser
将调用 subprocess.Popen()
与exe文件,将取得成功,即使有不好的URL,并返回真
。 IE浏览器没有给出问题的迹象,所有其他的浏览器有一个很好的消息说,他们无法找到该文件。
Both GenericBrowser
and BackgroundBrowser
will call subprocess.Popen()
with an exe which will succeed, even with a bad URL, and return True
. IE gives no indication of the issue, all other browsers have a nice messages saying they can't find the file.
-
GenericBrowser
是由环境变量设置浏览器
和第一。 -
WindowsDefault
是第二个。 -
BackgroundBrowser
是最后的,包括回退IE浏览器,如果没有别的工作。
GenericBrowser
is set by the environment variableBROWSER
and is first.WindowsDefault
is second.BackgroundBrowser
is last and includes the fall back IE if nothing else works.
下面是我原来的设置:
>>> 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>])]
>>>
下面是我的设置modifiying环境变量之后:
Here is my setup after modifiying the environment 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
,为浏览器的列表尝试。注册铬或添加浏览器的环境变量或modifiying我的所有路径会得到我一个更好的错误消息正确的浏览器。
The webbrowser._tryorder
gives the list of browsers tried. Registering chrome or adding a BROWSER env var or modifiying my path all would have gotten me the correct browser with a better error message.
感谢您的帮助球员,我不可能已经解决了这个没有你的想法。
Thanks for the help guys, I couldn't have solved this without your ideas.
这篇关于python的web浏览器在Windows 7推出,而不是默认的IE浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!