How to Get Domain Name from URL String in Python

April 11, 2016

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('')

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


And that’s it.

👋 I'm Ashik Nesin • Sr. Frontend Engineer @Chargebee 👨‍💻• I write about modern Javascript & building web apps

© 2021 Nesin Technologies LLP