webbrowser脚本无错误执行,但没有任何反应?

0 投票
1 回答
751 浏览
提问于 2025-04-17 18:34

我正在写一个脚本,目的是打开不同的浏览器,并访问给定的网址。

当我在Eclipse中运行这个脚本时,它没有报错,但就是没有打开任何浏览器。:/

import webbrowser as wb

url_mf = ['https://www.thatsite.com/','http://thatothersite.org/']
url_gc = ['https://www.thatsite.com/','http://thatothersite.org/']

chrome = wb.get('/usr/bin/google-chrome %s')
firefox = wb.get('fierfox %s')

chrome.open(url_gc[1], new=1)
firefox.open(url_mf[1], new=1)

我还有一个脚本,使用IEC.py模块来打开Internet Explorer(我需要输入登录信息,然后从一个网站提取格式很糟糕的数据库查询 - 用mechanize和selenium似乎有点过于复杂了?),这个脚本运行得很好。不过我想这就像是在比较苹果和橘子吧?

import iec
ie= iec.IEController()
ie.Navigate(url_ie[1])

非常感谢任何帮助。

1 个回答

1

我首先注意到第5行有个拼写错误。应该是Firefox,而不是fierfox。第二点,我在SublimeText 2中运行了你的代码,没有遇到任何问题,只是因为我在Windows电脑上,所以我改了路径。

下面的代码可以同时打开Firefox和Chrome。

import webbrowser as wb

url_mf = ['https://www.thatsite.com/','http://www.google.ie/']
url_gc = ['https://www.thatsite.com/','http://www.google.ie/']

chrome = wb.get('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %s')
firefox = wb.get('"C:/Program Files (x86)/Mozilla Firefox/firefox.exe" %s')

chrome.open(url_gc[1], new=1)
firefox.open(url_mf[1], new=1)

你真的想指定程序使用哪个浏览器吗?我建议使用

import webbrowser as wb

urls = ["http://www.google.ie/","http://www.gametrailers.com/"]

for url in urls:
    wb.open(url,new=2, autoraise=True)

这样做会直接打开你默认的浏览器,并在新标签页中打开每个链接。

撰写回答