Search code examples
pythonurlurlliburl-scheme

Comparing two URLs with and without https


Is there a way I can compare two URLs regardless of their protocol so that https://www.google.com and www.google.com/ are the same URL?


Solution

  • You could use urlparse:

    from urllib.parse import urlparse
    
    u1 = urlparse('https://www.google.com')
    u2 = urlparse('http://www.google.com/')
    u3 = urlparse('ftp://www.google.com/som/ting/else')
    
    print(u1.netloc == u2.netloc == u3.netloc)
    

    Out:

    True