How to Open a Url in Python

How to Open a Url in Python
import urllib

fun open():
    return urllib.urlopen(')

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(')

I am using bottle. Giving me the error:

TypeError("'bool' object is not iterable",)
5

7 Answers

with the webbrowser module

import webbrowser

webbrowser.open(')  # Go to example.com
1
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

On Windows

import os
os.system("start \"\" ")

On macOS

import os
os.system("open \"\" ")

On Linux

import os
os.system("xdg-open \"\" ")

Cross-Platform

import webbrowser

webbrowser.open(')

You have to read the data too.

Check out : 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.

You could also try:

import os
os.system("start \"\" ")

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

Here is another way to do it.

import webbrowser

webbrowser.open("foobar.com")

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

webbrowser.open_new_tab(url)
Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.