1

[Yes, the title is not a typo!]

In python I need something that parses a URL. I cannot believe something standard does not already exist. As the URL is set in a config I'd like to make sure it is not garbage.

There's urlparse.urlparse, but that only parses 'valid URLs' (with some invalid URLs sometimes raising an undocumented ValueError)

e.g.

>>> import urlparse
>>> urlparse.urlparse('http://aa :: aa ! aa:11.com:aa').netloc
'aa :: aa ! aa:11.com:aa'

shows how urlparse parses what I would consider an invalid URL.

2
  • Can't you just use urlparse and catch the errors? Commented Aug 15, 2012 at 10:31
  • Whats about a regex? I guees youll find thousands for urls. Commented Aug 15, 2012 at 10:32

1 Answer 1

2

URL parsing and URL validation are actually different tasks.

urlparse.urlparse makes parsing, validation is usually made using regular expressions machine (built-in re module in Python).

Here's and example of URL validation from Django framework:

regex = re.compile(
    r'^(?:http|ftp)s?://' # http:// or https://
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
    r'localhost|' #localhost...
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
    r'(?::\d+)?' # optional port
    r'(?:/?|[/?]\S+)$', re.IGNORECASE)
Sign up to request clarification or add additional context in comments.

2 Comments

I had assumed there was something standard, preferrable to just cut-n-pasting code. But, django.core.validators.URLValidator is exactly what I need, thanks :)
It is almost impossible to write an extensive url validator with regex. so many cases suc as tel: or mailto: are missing. Also IPv6 and many more...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.