Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *