How to Get Domain Name Information in Python?

Posted in /  

How to Get Domain Name Information in Python?

Vinay Khatri
Last updated on September 14, 2022

    There are many web applications present on the internet that will provide you the public information about any web domain . The information could be the domain IP address, expiration date, age of the domain, etc. Here in this Python tutorial, we will write a Python program that will get us information about any valid domain.

    For this tutorial, we will be using the Python whois library , which name is borrowed from the WHOIS query. The WHOIS query is the response protocol that is used to query databases that store information about the registered Internet resources, such as domain names and IP addresses.

    Required Library

    Before executing the program below, make sure that the python-whois library is installed for your Python environment. Run the following Python pip install command on your terminal(Linux/Mac) or command prompt(windows) to install the python-whois library.

    pip install python-whois

    Python Program to fetch the public information about the Domain Name

    import whois
    import datetime
    
    domain_name = input("Enter Domain Name: ")
    
    #for valid domain
    try:
        #whois query 
        response= whois.whois(domain_name)
    
        print("The Domain name's:", response.domain_name)
        print("Domain Created on: ", response.creation_date.strftime('%d %B %Y'))
        print("Domain Registered By: ", response.registrar)
        age = (datetime.datetime.today() - response.creation_date).days 
        print("The Age of Domain is: ", age,"days")
    
        print("Domain Expire date is:", response.expiration_date.strftime('%d %B %Y'))
        print("Domain Country code is:", response.country)
    
    #for invalid domain
    except:
        print(f"No domain found by {domain_name}")

    Output

    Enter Domain Name: techgeekbuzz.com
    The Domain name's: ['TECHGEEKBUZZ.COM', 'techgeekbuzz.com']
    Domain Created on: 31 December 2018
    Domain Registered By: GoDaddy.com, LLC
    The Age of Domain is: 758 days
    Domain Expire date is: 31 December 2022
    Domain Country code is: IN

    Conclusion

    In this Python tutorial, you learned how to get information about a domain using Python and the python-whois library. The WHOIS query can only fetch the domain data which is public.

    If you like this article and the above Python program, let us know by commenting down below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments