How do you validate a URL with a regular expression in Python?

There's no validate method as almost anything is a valid URL. There are some punctuation rules for splitting it up. Without any punctuation, you still have a valid URL.

Depending on the situation, we use following methods.

  • If you trust the data, and just want to verify if the protocol is HTTP, then urlparse is perfect.

  • If you want to make the URL is actually a true URL, use the cumbersome and maniacal regex

  • If you want to make sure it's a real web address, use the following code


import urllib
except IOError:
    print "Not a real URL"

Updated on: 13-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started