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

PythonServer Side ProgrammingProgramming

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

## Example

import urllib
try:
urllib.urlopen(url)
except IOError:
print "Not a real URL"
Published on 11-Jan-2018 23:24:49