132
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
  • 19,327
  • 10
  • 37
  • 52
shamsee
  • 1,329
  • 2
  • 10
  • 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
  • 1
    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

8 Answers8

272

with the webbrowser module

import webbrowser

webbrowser.open('http://example.com')  # Go to example.com
rmmh
  • 6,997
  • 26
  • 37
aaronasterling
  • 68,820
  • 20
  • 127
  • 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
  • 1
    @ewerybody It is always going to open your default browser. –  Oct 30 '22 at 17:02
  • Well, this is 7 years ago I'm pretty sure I had Firefox as default browser even back then. Now this ^ also works for me – ewerybody Nov 06 '22 at 20:34
  • 1
    [Quite some things](https://github.com/python/cpython/commits/main/Lib/webbrowser.py) happened on the module since then. – ewerybody Nov 06 '22 at 20:36
41
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,967
  • 2
  • 28
  • 40
36

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
  • 32,495
  • 27
  • 95
  • 175
Abraham
  • 8,525
  • 5
  • 47
  • 53
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
  • 65,343
  • 15
  • 148
  • 136
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://".

3

Here is another way to do it.

import webbrowser

webbrowser.open("foobar.com")
Arnav Poddar
  • 354
  • 2
  • 18
DocJ457
  • 797
  • 7
  • 17
1

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

webbrowser.open_new_tab(url)
Pang
  • 9,564
  • 146
  • 81
  • 122
0

For those who feel like not using webbrowser, here's my Windows-only solution with os instead:

import os # `os.path` is still available through `os`!

sOFile = os.path.join(os.environ["TEMP"], "so.url") # Put StackOverflow's .URL file in %temp%
with open(sOFile, "w") as urlIO:
    urlIO.write("[InternetShortcut]\nURL=https://stackoverflow.com")
os.startfile(sOFile)
os.remove(sOFile)
RixTheTyrunt
  • 185
  • 3
  • 11