To get the domain from a URL in Python, the easiest way is to use the urllib.parse module urlparse() function and access the netloc attribute.
from urlparse.parse import urlparse
domain = urlparse("https://daztech.com/python-get-domain-from-url").netloc
print(domain)
#Output:
daztech.com
When working with URLs in Python, the ability to easily extract information about those URLs can be very valuable.
One such piece of information which is valuable to have given a URL is the domain name.
We can use Python to easily get the domain of a URL using the urllib.parse module.
The urllib.parse module has the function urlparse() which parses a URL and returns an object with pieces of information about the URL such as scheme, domain, path, the query string, etc.
With urlparse(), you can get the domain from a URL. The domain will be stored in the return value’s ‘netloc’ attribute.
Below is a simple example of how you can get the domain from a URL using Python.
from urlparse.parse import urlparse
domain = urlparse("https://daztech.com/python-get-domain-from-url").netloc
print(domain)
#Output:
daztech.com
Using urlparse() Function to Get Other Pieces of Information about URLs in Python
The urlparse() function allows you to get other pieces of information of a URL. When you use urlparse(), you get back a 6-tuple which has information such as scheme, domain, path, the query string, etc.
Below is an example showing the information you will get back if you use urlparse() in your Python code.
from urlparse.parse import urlparse
print(urlparse("https://daztech.com/python-get-domain-from-url/"))
#Output:
ParseResult(scheme='https',netloc='daztech.com', path='/python-get-domain-from-url/", params='', query='', fragment=''
Hopefully this article has been useful for you to learn how to get the domain name from a URL with Python.