Get protocol + host name from URL
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams
--
Chapters
00:00 Question
01:30 Accepted answer (Score 359)
01:50 Answer 2 (Score 94)
02:33 Answer 3 (Score 52)
02:50 Answer 4 (Score 33)
03:02 Thank you
--
Full question
https://stackoverflow.com/questions/9626...
Accepted answer links:
[python2]: https://docs.python.org/2/library/urlpar...
[python3]: https://docs.python.org/3/library/urllib...
Answer 2 links:
https://github.com/john-kurkowski/tldext...
Answer 3 links:
[urlsplit]: http://docs.python.org/3/library/urllib....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams
--
Chapters
00:00 Question
01:30 Accepted answer (Score 359)
01:50 Answer 2 (Score 94)
02:33 Answer 3 (Score 52)
02:50 Answer 4 (Score 33)
03:02 Thank you
--
Full question
https://stackoverflow.com/questions/9626...
Accepted answer links:
[python2]: https://docs.python.org/2/library/urlpar...
[python3]: https://docs.python.org/3/library/urllib...
Answer 2 links:
https://github.com/john-kurkowski/tldext...
Answer 3 links:
[urlsplit]: http://docs.python.org/3/library/urllib....
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django
#avk47
ACCEPTED ANSWER
Score 374
You should be able to do it with urlparse (docs: python2, python3):
from urllib.parse import urlparse
# from urlparse import urlparse # Python 2
parsed_uri = urlparse('http://stackoverflow.com/questions/1234567/blah-blah-blah-blah' )
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)
# gives
'http://stackoverflow.com/'
ANSWER 2
Score 97
https://github.com/john-kurkowski/tldextract
This is a more verbose version of urlparse. It detects domains and subdomains for you.
From their documentation:
>>> import tldextract
>>> tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')
>>> tldextract.extract('http://forums.bbc.co.uk/') # United Kingdom
ExtractResult(subdomain='forums', domain='bbc', suffix='co.uk')
>>> tldextract.extract('http://www.worldbank.org.kg/') # Kyrgyzstan
ExtractResult(subdomain='www', domain='worldbank', suffix='org.kg')
ExtractResult is a namedtuple, so it's simple to access the parts you want.
>>> ext = tldextract.extract('http://forums.bbc.co.uk')
>>> ext.domain
'bbc'
>>> '.'.join(ext[:2]) # rejoin subdomain and domain
'forums.bbc'
ANSWER 3
Score 52
Python3 using urlsplit:
from urllib.parse import urlsplit
url = "http://stackoverflow.com/questions/9626535/get-domain-name-from-url"
base_url = "{0.scheme}://{0.netloc}/".format(urlsplit(url))
print(base_url)
# http://stackoverflow.com/
ANSWER 4
Score 35
>>> import urlparse
>>> url = 'http://stackoverflow.com/questions/1234567/blah-blah-blah-blah'
>>> urlparse.urljoin(url, '/')
'http://stackoverflow.com/'