有谁知道如何在 window 下使用 python webbrowser.open

Do anyone know how to use python webbrowser.open under window

我曾尝试使用 python 和 webbrowser.open,但它只适用于 IE。如何让它打开 chrome 或 firefox。我不希望它在 IE 上打开,我想在 Chrome 或 Firefox 上打开。由于我尝试了很多方法,但其中 none 有效。

import time
import webbrowser
webbrowser.open('www.google.com')

您需要指定您的 webbrowser's name,详情请参阅 webbrowser.get

import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True

更新
如果您的计算机中安装了 chromefirefox,请执行以下操作:

chrome_path =r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' # change to your chrome.exe path  
# webbrowser is just call subprocess.Popen, so make sure this work in your cmd firstly
# C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com

# there two way solve your problem
# you have change \ to / in windows
# this seems a bug in browser = shlex.split(browser) in windows
# ['C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe', '%s']
a = webbrowser.get(r'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe %s')
a.open('www.google.com')  #True
# or by register 
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'))
a = webbrowser.get('chrome')
a.open('www.google.com')  #True

否则你可以试试 selenium, it provide much more functions and only need chromedriver