0
url = 'http://www.google.org/'
chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
webbrowser.get(chrome_path)
webbrowser.open(url)  

above will open chrome, which is what I want.

However if I change the url to url = 'reddit it will open internet explore instead. Why does it open different webbrowsers for different urls? And how can I make sure it opens in chrome for all urls?

4

1 回答 1

1

做这个:

>>> import webbrowser
>>> browser = webbrowser.get()
>>> browser.open('http://google.com')
True
>>> browser.open_new_tab('http://yahoo.com')
True
>>>

webbrowser.get()调用将为您提供一个浏览器控制器对象。您可以在控制器对象上运行open,open_new和。open_new_tab这将确保命令在您打开的同一浏览器实例上执行。

如果您直接使用webbrowser.open()- 它将始终在默认浏览器中打开链接,在您的情况下是 Internet Explorer。

所以要重写你的代码:

chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
chrome = webbrowser.get(chrome_path)
chrome.open('http://google.com')
chrome.open_new_tab('http://reddit.com')
于 2016-07-10T09:57:12.667 回答