Bishwash
Bishwash

Reputation: 884

Comparing urls with slash and www

Are urls listed below the same or equal?

1. http://example.com
2. http://www.example.com
3. http://example.com/

How to compare those urls in python for equality if they are same?

Thanks in advance.

Upvotes: 2

Views: 1628

Answers (2)

Dmitry Vakhrushev
Dmitry Vakhrushev

Reputation: 1382

You can split up entire URL into parts:

>>> import urlparse
>>> urlparse.urlparse('http://www.example.com')
ParseResult(scheme='http', netloc='www.example.com', path='', params='', query='', fragment='')
>>> urlparse.urlparse('http://example.com')
ParseResult(scheme='http', netloc='example.com', path='', params='', query='', fragment='')

If you assume that all URLs are come from the same domain, but sometimes are prefixed with "www", you could just compare "path" component. It depends on your particular task.

Upvotes: 2

Mike
Mike

Reputation: 894

No need to reinvent the wheel.

Use this REGEX designed for parsing URLs.

http://daringfireball.net/2010/07/improved_regex_for_matching_urls

Upvotes: 0

Related Questions