Home  >  Q&A  >  body text

Python extract domain name from URL

How does Python extract domain name from URL? The url has various formats as follows:

enter:

https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1
https://stackoverflow.com/questions/1234567/blah-blah-blah-blah
http://www.domain.com
https://www.other-domain.com/whatever/blah/blah/?v1=0&v2=blah+blah ...

Output:

docs.google.com
stackoverflow.com
www.domain.com
www.other-domain.com
为情所困为情所困2669 days ago786

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-28 09:24:24

    Use Python’s built-in module urlparse

    from urlparse import *
    url = 'https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1'
    result = urlparse(url)

    result contains all the information of the URL

    reply
    0
  • ringa_lee

    ringa_lee2017-06-28 09:24:24

    Original source: Python practical script list

    Extract domain name from URL

    def extractDomainFromURL(url):
        """Get domain name from url"""
        from urlparse import urlparse
        parsed_uri = urlparse(url)
        domain = '{uri.netloc}'.format(uri=parsed_uri)
        return domain

    reply
    0
  • Cancelreply