1

I am new to Python and it seems to have a lot of nice functions that I don't know about. What function can I use to get the root site name? For example, how would I get faqs.org if I gave the function the URL "http://www.faqs.org/docs/diveintopython/kgp_commandline.html"?

2
  • docs.python.org/library/urlparse.html Commented Feb 3, 2009 at 18:02
  • Just to clarify, do you really want just "faqs.org" or do you want "www.faqs.org"? Trying to get the former is a Bad Idea, as it isn't portable to domains like "www.amazon.co.uk". Commented Feb 3, 2009 at 18:12

3 Answers 3

5
 >>> from urllib.parse import urlparse
 >>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html').hostname
 'www.cwi.nl'
Sign up to request clarification or add additional context in comments.

Comments

2

The much overlooked urlparse module:

from urlparse import urlparse
scheme, netloc, path, params, query, fragment = urlparse("http://www.faqs.org/docs/diveintopython/kgp_commandline.html")
print netloc

1 Comment

netloc might includes port number
2

What version of Python are you learning with? Note that SilentGhost's answer is for Python 3.0, while Alabaster Codify's will work with the 2.x series.

Comments

Your Answer

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