91
import urllib

fun open():
    return urllib.urlopen('http://example.com')

But when example.com opens it does not render CSS or JavaScript. How can I open the webpage in a web browser?

@error(404)
def error404(error):
    return webbrowser.open('http://example.com')

I am using bottle. Giving me the error:

TypeError("'bool' object is not iterable",)
Syscall
  • 16,959
  • 9
  • 22
  • 41
shamsee
  • 919
  • 1
  • 8
  • 6
  • It looks like we have a language disconnect. the `error` decorator seems to be expecting an iterable. `webbrowswer.open` will _open a url in the browser_ and return `True` or `False`, preserving [command-query-separation](http://en.wikipedia.org/wiki/Command-query_separation). You don't actually want to open this page in the browswer, do you? – aaronasterling Nov 29 '10 at 08:54
  • Do you want to download the javascript and css after you've already downloaded the html? – aaronasterling Nov 29 '10 at 09:00
  • I concern is to open url in browser. If it is possible with downloding the js and css then it is OK. – shamsee Nov 29 '10 at 09:06
  • just open the python interpreter and type `webbrowser.open('http://www.google.com')` and see if it does what you want. – aaronasterling Nov 29 '10 at 09:16
  • yes. The result is same. But it always opens in mozilla. – shamsee Nov 29 '10 at 09:33

7 Answers7

200

with the webbrowser module

import webbrowser

webbrowser.open('http://example.com')  # Go to example.com
rmmh
  • 6,641
  • 22
  • 37
aaronasterling
  • 61,516
  • 19
  • 116
  • 125
  • What the.. this always opens the internet explorer?!!!? D: For the time being I used subprocess to call an explorer with the url as argument. This always uses the "default web browser" but also opens an explorer instance... hmmm – ewerybody Jun 12 '15 at 18:46
31
import webbrowser  
webbrowser.open(url, new=0, autoraise=True)

Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised

webbrowser.open_new(url)

Open url in a new window of the default browser

webbrowser.open_new_tab(url)

Open url in a new page (“tab”) of the default browser

imp
  • 1,575
  • 1
  • 24
  • 38
20

On Windows

import os
os.system("start \"\" https://example.com")

On macOS

import os
os.system("open \"\" https://example.com")

On Linux

import os
os.system("xdg-open \"\" https://example.com")

Cross-Platform

import webbrowser

webbrowser.open('https://example.com')
Fantastic Mr Fox
  • 27,453
  • 22
  • 81
  • 151
abranhe
  • 4,384
  • 1
  • 29
  • 41
10

You have to read the data too.

Check out : http://www.doughellmann.com/PyMOTW/urllib2/ to understand it.

response = urllib2.urlopen(..)
headers = response.info()
data = response.read()

Of course, what you want is to render it in browser and aaronasterling's answer is what you want.

pyfunc
  • 60,253
  • 14
  • 138
  • 132
8

You could also try:

import os
os.system("start \"\" http://example.com")

This, other than @aaronasterling ´s answer has the advantage that it opens the default web browser. Be sure not to forget the "http://".

2

Here is another way to do it.

import webbrowser

webbrowser.open("foobar.com")
Arnav Poddar
  • 338
  • 2
  • 17
David Odhiambo
  • 539
  • 5
  • 14
1

I think this is the easy way to open a URL using this function

webbrowser.open_new_tab(url)
Pang
  • 8,605
  • 144
  • 77
  • 113