python 的 webbrowser 在 Windows 相对路径上启动 IE,而不是默认浏览器 [英] python's webbrowser launches IE, instead of default browser, on Windows relative path

查看:20
本文介绍了python 的 webbrowser 在 Windows 相对路径上启动 IE,而不是默认浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在默认浏览器中从 python 启动本地 html 文件(现在我的默认浏览器是 Google Chrome,如果我双击一个 .html 文件,Chrome 就会启动.)

当我使用 python 的 webbrowser.open() 时,IE 会启动,但地址栏为空白.

Python 2.7.1 (r271:86832, 2010 年 11 月 27 日,17:19:03) [MSC v.1500 64 位 (AMD64)] on win32输入帮助"、版权"、信用"或许可证"想要查询更多的信息.>>>导入浏览器>>>文件名 = 'test.html'>>>webbrowser.open('file://'+文件名)真的>>>打印(webbrowser.get().__class__.__name__)视窗默认值

我检查了我的默认程序,它们看起来是正确的.我在 Win 7 SP1 上.为什么 Chrome 无法启动?

更新:代码将在未知的操作系统和机器上运行,因此硬编码或注册浏览器或路径更新不是选项.我想解析 file:// 的 url,然后做一个 os.path.exists 检查和 os.path.realpath可能就是答案.

解决方案

我的主要问题是一个错误的 URL,尝试将 file:// 前置到相对路径.可以通过以下方式修复:

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

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

WindowsDefault 类调用 os.startfile() 失败并返回 False.我可以通过在 windows run 命令中输入 URL 并查看错误消息而不是浏览器来验证这一点.

GenericBrowserBackgroundBrowser 都将使用 exe 调用 subprocess.Popen(),即使 URL 错误,该 exe 也会成功,并返回.IE 没有给出这个问题的指示,所有其他浏览器都有一个很好的消息,说他们找不到该文件.

  1. GenericBrowser 由环境变量 BROWSER 设置,并且是第一个.
  2. WindowsDefault 是第二个.
  3. BackgroundBrowser 最后一个,如果没有其他效果,则包括后备 IE.

这是我的原始设置:

<预><代码>>>>导入浏览器>>>webbrowser._tryorder['windows-默认','C:\Program Files\Internet Explorer\IEXPLORE.EXE']>>>webbrowser._browsers.items()[('windows-default', [, None]),('c:\program files\internet explorer\iexplore.exe', [None, <webbrowser.BackgroundBrowser object at 0x00000000022E3898>])]>>>

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

C:>path=C:Program Files (x86)Mozilla Firefox;%path%C:>set BROWSER=C:UsersScottAppDataLocalGoogleChromeApplicationchrome.exeC:>蟒蛇Python 2.7.1 (r271:86832, 2010 年 11 月 27 日,17:19:03) [MSC v.1500 64 位 (AMD64)] 在 win32 上输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入浏览器>>>webbrowser._tryorder['C:\Users\Scott\AppData\Local\Google\Chrome\Application\chrome.exe','windows-默认','火狐','C:\Program Files\Internet Explorer\IEXPLORE.EXE']>>>webbrowser._browsers.items()[('windows-default', [, None]),('c:\program files\internet explorer\iexplore.exe',[None, ]),('firefox', [None, ]),('c:\users\scott\appdata\local\google\chrome\application\chrome.exe', [None, <webbrowser.GenericBrowser object at 0x000000000235E8D0>])]>>>

webbrowser._tryorder 给出尝试过的浏览器列表.注册 chrome 或添加 BROWSER env var 或修改我的路径都会让我得到正确的浏览器,并显示更好的错误消息.

感谢各位的帮助,如果没有你们的想法,我无法解决这个问题.

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

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

I've checked my default programs and they look correct. I'm on Win 7 SP1. Why is Chrome not launching?

Update: The code will be running on unknown OS's and machines, so hardcoding or 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.

解决方案

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

Using webbrowser.open will try multiple methods until one "succeeds", which is a loose definition.

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.

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.

  1. GenericBrowser is set by the environment variable BROWSER and is first.
  2. WindowsDefault is second.
  3. BackgroundBrowser is last and includes the fall back IE if nothing else works.

Here is my original setup:

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

Here is my setup after modifiying the environment variables:

C:>path=C:Program Files (x86)Mozilla Firefox;%path%

C:>set BROWSER=C:UsersScottAppDataLocalGoogleChromeApplicationchrome.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>])]
>>>

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 的 webbrowser 在 Windows 相对路径上启动 IE,而不是默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆