[Tutor] Selecting a browser

Ricardo Aráoz ricaraoz at gmail.com
Mon Dec 3 14:19:20 CET 2007


Martin Walsh wrote:
> 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')
> 

Hi Marty, thanks for your help.
I've tried your suggestions but they don't seem to work for me. In W's
system window I can do :
C:/> S:\FirefoxPortable\FirefoxPortable.exe http://www.google.com
and it will open my browser ok. But no matter what I try :
"c:/program files/mozilla firefox/firefox.exe %s &" or "c:/program
files/mozilla firefox/firefox.exe %s" as input to webbrowser.get() it
won't work.
Here's my session :

>>> import webbrowser
>>> ff = webbrowser.get("S:\FirefoxPortable\FirefoxPortable.exe %s &")
>>> ff.open('http://www.google.com')
False
>>> ff = webbrowser.get("S:\FirefoxPortable\FirefoxPortable.exe %s &")
>>> ff
<webbrowser.BackgroundBrowser object at 0x01BF7AD0>
>>> ff.open(r'http://www.google.com')
False
>>> ff.open('')
False
>>> ff = webbrowser.get("S:\FirefoxPortable\FirefoxPortable.exe %s")
>>> ff.open(r'http://www.google.com')
False

Besides looking at the Python 2.5 documentation gave me no clues. It is
very poorly documented e.g. :

register( name, constructor[, instance])
	Register the browser type name. Once a browser type is registered, the
get() function can return a controller for that browser type. If
instance is not provided, or is None, constructor will be called without
parameters to create an instance when needed. If instance is provided,
constructor will never be called, and may be None.
	This entry point is only useful if you plan to either set the BROWSER
variable or call get with a nonempty argument matching the name of a
handler you declare.

But it does not define what a 'constructor' is (I guess a function, but
no clue about what it's signature or functionality should be) or what an
"instance" is (but I can guess). Trouble is when you guess and things
don't work you don't have a clue if the fault lies in your 'guess' or in
your code. Note that it mentions 'the BROWSER variable' but does not say
what it is, what it's value should be, nor any clue about it's use.
Probably I lack knowledge in the subject, but hey! the whole purpose of
this module is to be able to call the browser WITHOUT any knowledge of
the subject.
I guess I'll have to look at the code when I have a couple of free hours.
I'd rather go with the 'webbrowser' module than the subprocess.Popen
option, as it 'should' abstract me from browser management. Anyway if I
can't advance through webbrowser I'll give your examples a try.
Thanks a lot for your help.

Ricardo

> 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
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list