Oct. 02, 2012

Using the web browser in Python

The webbrowser module in Python provides an interface to display Web-based documents.


Under most circumstances, simply calling the open() function from this module
will do the right thing. 

IN Unix, graphical browsers are preferred under X11, but text-mode browsers
will be used if graphical browsers are not available or an X11 display 
isn’t available. 

If text-mode browsers are used, the calling process will block until the user
exits the browser.

Usage Examples

    Open url in a new window of the default browser, if possible, otherwise,
    open url in the only browser window.

    Open url in a new page (“tab”) of the default browser, if possible, 
    otherwise equivalent to open_new().

Webbrowser Script

This example will ask the user to enter a search term. 

A new browser tab will open with the search term in googles search field.

import webbrowser
google = raw_input('Google search:')
webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=%s' % google)
The script webbrowser can be used as a command-line interface for the module. 

It accepts an URL as the argument. 

It accepts the following optional parameters: 
	-n opens the URL in a new browser window, 
	-t opens the URL in a new browser page (“tab”)

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.