from urllib.parse import urlparse domain = urlparse('http://www.example.test/foo/bar').netloc print(domain) # --> www.example.test