Python webbrowser.open() 打开 Chrome 浏览器 [英] Python webbrowser.open() to open Chrome browser

查看:122
本文介绍了Python webbrowser.open() 打开 Chrome 浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档http://docs.python.org/3.3/library/webbrowser.html 它应该在默认浏览器中打开,但由于某种原因,它在我的机器上打开了 IE.我进行了谷歌搜索,发现一个答案说我需要注册浏览器,但我不确定如何使用 webbrowser.register() 并且文档似乎不是很清楚.如何注册 Chrome,以便我传递给 webbrowser.open() 的 URL 在 Chrome 而不是 IE 中打开?

According to the documentation http://docs.python.org/3.3/library/webbrowser.html it's supposed to open in the default browser, but for some reason on my machine it opens IE. I did a google search and I came across an answer that said I need to register browsers, but I'm not sure how to use webbrowser.register() and the documentation doesn't seem to be very clear. How do I register Chrome so that urls I pass to webbrowser.open() open in Chrome instead of IE?

推荐答案

您可以使用 Chrome 的路径调用 get().下面是一个示例 - 将 chrome_path 替换为适用于您平台的正确路径.

You can call get() with the path to Chrome. Below is an example - replace chrome_path with the correct path for your platform.

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)

这篇关于Python webbrowser.open() 打开 Chrome 浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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