使用Python在Firefox(win)选项卡上启动一个网页

27

我尝试使用Python在新选项卡中打开网站URL,但用以下两种方法都没有成功:

方法1:

os.system('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');

方法2:

os.startfile('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');

如果我不加参数(-new-tab http://www.google.com/),它会正常工作,打开默认页面。

11个回答

62
你需要使用 webbrowser 模块。
import webbrowser
webbrowser.open('http://www.google.com')

[编辑]

如果你想在非默认浏览器中打开一个URL,请尝试以下操作:

webbrowser.get('firefox').open_new_tab('http://www.google.com')

4
如果用户的默认浏览器不是Firefox,你应该强制他们使用吗? - John Fouhy
1
@Leandro 我把调用open_new_tab的代码改了,现在应该可以打开一个新标签页了。 - Nadia Alramli
1
啊,我注意到它在Windows上不起作用,出现了这个错误:“无法找到可运行的浏览器”。 - Leandro Ardissone
1
嗯,这里有人问同样的问题http://echochamber.me/viewtopic.php?f=20&t=23436 模块本身应该在Windows上工作。 - Nadia Alramli
1
就我个人而言,我只需要将mozilla\firefox安装目录添加到我的PATH中--不需要任何其他环境变量即可使webbrowser与Firefox和Python 2.7配合使用。 - martineau
显示剩余8条评论

12

如果你想要使用参数启动一个程序,subprocess 模块更适合:

import subprocess
subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe',
    '-new-tab', 'http://www.google.com/'])

4
如果没有安装Firefox会怎样?如果它被安装在不同的目录下会怎样?这不是正确的做法。即使在某些情况下它能够工作。 - Nadia Alramli

3

使用os.startfile(),只需传递URL即可。这将在用户默认的浏览器中打开一个新的选项卡/窗口,对您的用户更友好。


同样的问题,我需要在 Firefox 中打开它而不是默认浏览器。 - Leandro Ardissone

1

你可以尝试以下方法:

import os
os.spawnl(os.P_NOWAIT, r'C:\Program Files\Mozilla Firefox\Firefox.exe',
          r'FireFox', '-new-tab', 'http://www.google.com/')

1

你可以在网页浏览器中使用Mozilla类:

import webbrowser
firefox = webbrowser.Mozilla("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
firefox.open('http://www.google.com')

1

有多种方式可以使用不同的包在Python中打开URL-
使用Selenium包-

from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/Users/abcarp/bin/chromedriver')
browser.get('https://in.linkedin.com/')
sleep(10)
browser.close()

下载Firefox驱动并将其放置在用户/用户名/bon位置,并将名称更改为firefox。
使用子进程包-
import subprocess
p = subprocess.Popen([r"/Volumes/Firefox/Firefox.app", "http://www.google.com"]) 
p.kill()

使用 mechanize 包 -
import mechanize
br = mechanize.Browser()
br.open("http://machinelearningstories.blogspot.com/")
br.close()

使用 web-browser 包 -
import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.google.com')

关闭已打开的网页。
import os
os.system("taskkill /im chrome.exe /f")    #( windows)
os.system("pkill -f Chrome")    # mac

这里提供了更详细的相同信息 - http://pythonfordatabuggers.blogspot.com/2020/04/automatically-open-and-do-some-actions.html


0
如果您正在Windows 7机器上使用Python 2.7(我的设置),如果您使用以下命令:
webbrowser.open('google.com')

它将打开传统的Windows资源管理器(是的,我知道...)。

但是,如果您使用:

webbrowser.open('http://google.com')

它将在您的默认网络浏览器中加载URL,在我的情况下是Firefox。


0

在不使用Internet Explorer的情况下打开链接并使用Firefox,只需确保Firefox是默认的Web浏览器。

import webbrowser


http = 'http://'
links = input()
b = webbrowser.open_new(http + links)

0
import os

os.chdir('C:\Program Files\Mozilla Firefox')    #address of exe file

os.system('firefox.exe')   # name of exe file

4
请您提供更详细的回答,不仅仅是代码。 - alan.elkin

0

作为 Firefox 命令帮助 firefox --help

--new-tab <url>    Open <url> in a new tab.

所以使用--new-tab而不是-new-tab


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接