How to Get Domain Name Information in Python

Published on Aug. 22, 2023, 12:16 p.m.

To get domain name information in Python , you can use the third-party python-whois library which provides a wrapper around the WHOIS command. Here’s an example code that demonstrates how to use the python-whois to get information about a domain:

import whois

def get_domain_info(domain_name):
    domain_info = whois.whois(domain_name)
    print(f"Domain name: {domain_info.domain_name}")
    print(f"Registrar: {domain_info.registrar}")
    print(f"Creation date: {domain_info.creation_date}")
    print(f"Expiration date: {domain_info.expiration_date}")
    print(f"Name servers: {domain_info.name_servers}")

get_domain_info("example.com")

In this code, we first import the whois module and define a function get_domain_info() which takes a domain name as input. We then use the whois.whois() function to retrieve the WHOIS information for the domain, and we print out various pieces of information such as the domain name, registrar, creation date, expiration date, and name servers.

Note that the information returned by the whois module may vary depending on the domain registrar and the domain name itself. Also, the whois module only supports certain top-level domains (TLDs), so it may not be able to retrieve information for all domains.

To install the python-whois library

To install the python-whois library, you can use the pip package manager which is included by default with most Python installations. Here are the steps to install python-whois:

  1. Open a terminal or command prompt.
  2. Type pip install python-whois and press Enter.
  3. Wait for the installation to complete. You should see a message that says “Successfully installed python-whois”.

If you are using a virtual environment for your Python project, you may want to activate the virtual environment before running the pip install command.

Keep in mind that the python-whois library requires the whois command to be installed on your system in order to function properly. If the whois command is not available, you may need to install it separately, depending on your operating system.

Tags:

related content