Search code examples
pythonpython-2.7python-webbrowser

Python webbrowser - Open a url without https://


I am trying to get python to open a website URL. This code works.

import webbrowser
url = 'http://www.example.com/'
webbrowser.open(url)

I have noticed that python will only open the URL is it has https:// at the beginning.

Is it possible to get python to open the URL if it's in any of the formats in the examples below?

url = 'http://www.example.com/'
url = 'https://example.com/'
url = 'www.example.com/'
url = 'example.com/'

The URLs will be pulled from outside sources so I can't change what data i receive.

I have looked at the python docs, and can't find the answer on stackoverflow.


Solution

  • Why not just add it?

    if not url.startswith('http')
        if url.startswith('www'):
            url = "http://" + url
        else
            url = "http://www." + url