[Tutor] Selecting a browser
Martin Walsh
mwalsh at groktech.org
Sun Dec 2 06:37:39 CET 2007
Ricardo Aráoz wrote:
> Hi, I've checked webbrowser module and so far I find no way of selecting
> a browser other than the default one. Say I want certain places opened
> with IE and others with Mozilla, and I don't want to mess with the
> user's setting of the default browser. Any tips?
> TIA
I think one would normally use the form webbrowser.get('firefox'), on
unix systems. But if I understand correctly, the "problem" with the
webbrowser module on windows (and perhaps it is similar on a mac) is
that unless the program can be found on your system PATH, only a generic
'windows-default' browser class is registered, which uses os.startfile,
releasing control to the os, and serves to open the url in the user's
default browser.
If you're determined to use the webbrowser module on windows, you might
be able to do something like this:
import webbrowser
ffcommand = "c:/program files/mozilla firefox/firefox.exe %s &"
ff = webbrowser.get(ffcommand)
ff.open("http://www.example.com")
iecommand = "c:/program files/internet explorer/iexplore.exe %s &"
ie = webbrowser.get(iecommand)
ie.open("http://www.example.com")
I suppose you could also register them manually for later use with the
webbrowser.get(browser_name) form.
webbrowser.register('firefox', None, ff)
webbrowser.get('firefox').open('http://example.com')
Personally, I would probably just cut out the middle module and use
subprocess.Popen to start the browser, after checking if it is installed
(with os.path.isfile, or similar) -- which seems to be, more or less,
what the webbrowser module does if it finds one of the predefined
browsers on your system PATH. Something like this (pseudo-code):
browser = 'c:/program files/mozilla firefox/firefox.exe'
if os.path.isfile(browser):
p = subprocess.Popen([browser, 'http://www.example.com'])
# if you want to wait for the browser to
# close before continuing use p.wait() here
else:
... web browser not found ...
For dispatching based on site (url, or some other criteria), one idea
would be to wrap something like the above in a function which accepts
the web browser program path as an argument, and then pass the function
a path appropriate for the given criteria. Here is another (untested)
example to demonstrate:
import subprocess
import urlparse
import sys, os
FFPATH = 'c:/program files/mozilla firefox/firefox.exe'
IEPATH = 'c:/program files/internet explorer/iexplore.exe'
IESITES = ['microsoft.com', 'www.microsoft.com']
def launch(url, browser, wait=False):
if os.path.isfile(browser):
p = subprocess.Popen([browser, url])
if wait:
p.wait()
else:
print 'Invalid browser.'
def main(url):
# pick browser path by domain name
netloc = urlparse.urlparse(url)[1]
if netloc in IESITES:
launch(url, IEPATH)
else:
launch(url, FFPATH)
if __name__ == '__main__':
if sys.argv[1:]:
main(sys.argv[1])
else:
print 'Not enough arguments.'
In theory, if you run this script from a console on windows with any
microsoft.com url as an argument, it should open in IE -- where all
others open in firefox. Really rough, but I hope it helps.
Regards,
Marty
More information about the Tutor
mailing list