如何在 python 3 中使用指定的 Web 浏览器打开 URL

son*_*d10 2 url python-3.x

我正在 python 3 中创建一个程序,我希望用户使用指定的 Web 浏览器应用程序打开一个 URL。我尝试使用 ,subprocess.Popen([application, URL])但它引发了一个FileNotFoundError. 任何帮助是极大的赞赏。

编辑:我忘了提及我使用的是 Windows 10,这是我收到的错误消息的副本:

Traceback (most recent call last):
File "C:\Users\[Name]\Desktop\AI.py", line 221, in <module>
subprocess.Popen(["google-chrome", "www.google.co.uk/"])
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

EDIT2:如果我尝试运行,这是我的结果subprocess.Popen(["start", "chrome", "www.example.com/"])(如果我省略"start",了数组的一部分,我会得到同样的错误):

`Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    subprocess.Popen(["start", "chrome", "http://www.google.co.uk/"])
  File "C:\Python34\lib\subprocess.py", line 859, in __init__ restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified`
Run Code Online (Sandbox Code Playgroud)

MRa*_*dev 6

或者你可以使用“webbrowser”模块

    import webbrowser

    url = 'www.google.com'

    webbrowser.open_new(url)
Run Code Online (Sandbox Code Playgroud)

它将使用您的默认浏览器,无需查找您机器的特定浏览器路径。使其在您的工作空间之外变得更加通用和可用。