1

Possible Duplicate:
Python extract domain name from URL

I'm thinking about the best way to get a naked domain from an host.

def naked_domain(host):
    """Returns a naked domain from an host.

    Doctests:
      >>> naked_domain("google.com")
      'google.com'
      >>> naked_domain("www.google.com")
      'google.com'
      >>> naked_domain("xxx.www.google.com")
      'google.com'
    """
    pass
Community
  • 1
  • 1
sahid
  • 2,570
  • 19
  • 25

1 Answers1

2

The urlparse module can split it up into it's components. You can then access the domain with 'netloc'.

Micah Carrick
  • 9,967
  • 3
  • 31
  • 43
  • from the OP's examples, he wants the url stripped to hostname.tld, without the subdomains. urlparse is unable to do that. The only way to get what he wants is to use a list of valid TLDs. This problem has been answered here: http://stackoverflow.com/questions/1066933/python-extract-domain-name-from-url/1069780#1069780 – Acorn Sep 12 '11 at 13:00
  • OP is looking to split full domain, not just get full domain from netloc – Corey Goldberg Sep 12 '11 at 13:02