Menu Close

How to get the protocol + host name from URL with Python?

To get the protocol + host name from URL with Python, we can use the urlparse function.

For instance, we write

from urllib.parse import urlparse

parsed_uri = urlparse('http://example.com/questions/1234567/blah-blah-blah-blah' )
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)

to call urlparse with a URL.

And then we get the protocol with uri.scheme.

And the host name is stored in the uri.netloc property.

Posted in Python, Python Answers