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 • Principal Software Engineer @Chargebee 👨‍💻• I write about web development & behinds the scenes of startup/side projects that I'm building

© 2022 Nesin Technologies LLP