In this tutorial, we will be building a Python program to extract information from a domain name such as the registrar, expiry, creation date, name servers, and many more.
To begin we first need to install a module called python-whois. Now we need to run the command below to install it.
pip install python-whois
Next, create a file called test-domain.py and import our module.
import whois
Now let’s create a class to extract all the information we need from a domain name.
class DomainInfo:
def __init__(self, domain):
self.domain = domain
self.whois_info = whois.whois(domain)
def get_expiration_date(self):
return self.whois_info.expiration_date
def get_creation_date(self):
return self.whois_info.creation_date
def get_updated_date(self):
return self.whois_info.updated_date
def get_domain_name(self):
return self.whois_info.domain_name
def get_name_servers(self):
return self.whois_info.name_servers
def get_registrar(self):
return self.whois_info.registrar
if __name__ == '__main__':
domain = input("Enter the domain : ")
domain_info = DomainInfo(domain)
print("Expiration date : ", domain_info.get_expiration_date())
print("Creation date : ", domain_info.get_creation_date())
print("Updated date : ", domain_info.get_updated_date())
print("Domain name : ", domain_info.get_domain_name())
print("Name Servers : ", domain_info.get_name_servers())
print("Registrar : ", domain_info.get_registrar())
In the code above we get the expiration date, creation date, domain name, name servers, and the registrar.
Run
Open your terminal and run the command below.
python3 test-domain.py
After the command above runs you should see the information displayed.
Results
Enter the domain : youtube.com
Expiration date : [datetime.datetime(2024, 2, 15, 5, 13, 12), datetime.datetime(2024, 2, 15, 0, 0)]
Creation date : 2005-02-15 05:13:12
Updated date : 2023-01-14 09:25:19
Domain name : ['YOUTUBE.COM', 'youtube.com']
Name Servers : ['NS1.GOOGLE.COM', 'NS2.GOOGLE.COM', 'NS3.GOOGLE.COM', 'NS4.GOOGLE.COM', 'ns2.google.com', 'ns1.google.com', 'ns3.google.com', 'ns4.google.com']
Registrar : MarkMonitor, Inc.
There you have it, Thanks for reading. Happy Coding.