How to Get Domain Name from URL String in Python

You might have found yourself in the situation where you need to get a domain name from a URL string. Let’s say you’re building a Web Crawler or a Scrapper.

In Python, we can pretty easily do it

We are going to use tldextract package to make our life simpler.

If you don’t have that package installed in your system/environment make sure to install it.

pip install tldextract

And here is a simple script to split the domain name from a URL string.

import tldextract

list = tldextract.extract('http://blog.ashiknesin.com/about')

domain_name = list.domain + '.' + list.suffix

# ashiknesin.com

And that’s it.