0

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.

2
  • This works on my machine with http://www.example.org. Maybe close your open browsers and try again?
    – user559633
    CommentedAug 20, 2015 at 14:12
  • why can't you change the url if you pull it from an outside source? It's quite easy to changeCommentedAug 20, 2015 at 14:12

2 Answers 2

1

Why not just add it?

if not url.startswith('http') if url.startswith('www'): url = "http://" + url else url = "http://www." + url 
2
  • @ThomasWagenaar The first if statement will work for both.
    – amza
    CommentedAug 20, 2015 at 14:17
  • aww yeah forget that, nice.CommentedAug 20, 2015 at 14:17
0

If you really don't want to change the url string (which is quite fast and easy) like stazima said, then you can use Python 3. It supports all the listed url types in your question (tested them).

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.