How to Get Domain Name Information in Python

Learn how to validate domain names using WHOIS, as well as getting domain name information such as domain registrar, creation date, expiration date and more in Python.
  · 3 min read · Updated jul 2020 · Web Scraping


A domain name is a string identifying a network domain, it represents an IP resource, such as a server hosting a website, or just a computer that has an access to the Internet.

In simple terms, what we know as the domain name is the address of your website that people type in the browser URL to visit it.

In this tutorial, we will use whois library in Python to validate domain names and retrieve various domain information such as creation and expiration date, domain registrar, address and country of the owner and more.

To get started, let's install the library:

pip3 install python-whois

WHOIS is a query and response protocol that is often used for querying databases that store registered domain names. It stores and delivers the content as a human readable format. whois library simply queries a WHOIS server directly instead of going through an intermediate web service.

Validating Domain Names

In this section, we'll use whois to tell whether a domain name exists and is registered, the below function does that:

import whois # pip install python-whois

def is_registered(domain_name):
    """
    A function that returns a boolean indicating 
    whether a `domain_name` is registered
    """
    try:
        w = whois.whois(domain_name)
    except Exception:
        return False
    else:
        return bool(w.domain_name)

whois.whois() function raises an exception for domains that doesn't exist, and may return without raising any exception even if the domain isn't registered, that is why we check whether domain_name exists, let's test this function:

# list of registered & non registered domains to test our function
domains = [
    "thepythoncode.com",
    "google.com",
    "github.com",
    "unknownrandomdomain.com",
    "notregistered.co"
]
# iterate over domains
for domain in domains:
    print(domain, "is registered" if is_registered(domain) else "is not registered")

We have defined some known domains and others that doesn't exist, here is the output:

thepythoncode.com is registered
google.com is registered
github.com is registered
unknownrandomdomain.com is not registered
notregistered.co is not registered

Awesome, in the next section, we'll see how to get various useful information about domain names.

Getting Domain WHOIS Information

It is pretty straightforward to use this library, we just pass the domain name to the whois.whois() function:

import whois

if is_registered(domain_name):
    whois_info = whois.whois(domain_name)

Now to get the domain registrar (company that manages the reservation of domain names), we simply access the attribute registrar:

    # print the registrar
    print("Domain registrar:", whois_info.registrar)

Getting the WHOIS server:

    # print the WHOIS server
    print("WHOIS server:", whois_info.whois_server)

Domain creation and expiration date:

    # get the creation time
    print("Domain creation date:", whois_info.creation_date)
    # get expiration date
    print("Expiration date:", whois_info.expiration_date)

Output:

Domain registrar: MarkMonitor Inc.
WHOIS server: whois.markmonitor.com        
Domain creation date: 1997-09-15 04:00:00  
Expiration date: 2028-09-14 04:00:00 

To see other various WHOIS information such as name servers, country, city, state, address, etc, just print whois_info:

    # print all other info
    print(whois_info)

Conclusion

There you have it! You just learned the easiest and quickest way to get domain name information in Python. Check python-whois Github repository.

Learn alsoHow to Make a Subdomain Scanner in Python.

Happy Learning ♥

View Full Code
Sharing is caring!



Read Also





Comment panel