This page describes how to obtain domain information by using the WHOIS Python library.
WHOIS is an Internet protocol for retrieving a record of domain name registration information. The Python WHOIS package uses Python to query and parse domain registration information. The registration information that can be obtained using the WHOIS Python library includes whether the domain is registered, the domain registrar, the domain creation date, the domain expiration date, and the address of the domain owner.
The Python WHOIS package should be used when there is a need to obtain information about a domain name. The Python WHOIS package functions can be run individually to obtain specific domain name information or can be included within a larger Python program.
The Python WHOIS package must be installed to utilize the domain name lookup functions. The latest version of Python 3 is required to run the Python WHOIS package. The PIP3 Python package manager is required to install the Python WHOIS package. PIP3 is included with the latest version of Python 3.
Use PIP3 to Install the Python WHOIS package by running the following in a terminal:
pip3 install python-whois
Use an import statement to install the Python WHOIS package at the beginning of the Python code:
import whois
Use the code below to complete each task using the WHOIS Python package. The information may not be filed with the domain registrar if a validated domain does not return the requested information.
Create the check_registration
function using the Python WHOIS package to determine if a domain name is registered:
def check_registration(domain_name):
try:
w = whois.whois(domain_name)
except Exception:
return False
else:
return bool(w.domain_name)
Use the check_registration
function to validate registration of a list of domains by using an if else statement:
domains = [
"amazon.com",
"github.com",
"notadomain.com",
"thisisntregistered.net"
]
for domain in domains:
print(domain, "is registered" if check_registration(domain) else "is not registered")
Use the check_registration
function to return the domain name registrar:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print("Domain registrar:", whois_info.registrar)
Use the check_registration
function to return the domain name creation date:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print("Domain creation date:", whois_info.creation_date)
Use the check_registration
function to return the domain name expiration date:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print("Expiration date:", whois_info.expiration_date)
Use the check_registration
function to return the domain name registration email address:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print("email addresses:", whois_info.emails)
Use the check_registration
function to return the domain name registration location:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print("address:", whois_info.address)
print("city:", whois_info.city)
print("state:", whois_info.state)
print("zip code:", whois_info.zipcode)
print("country:", whois_info.country)
Use the check_registration
function to return all domain name registration information:
domain_name = "github.com"
if check_registration(domain_name):
whois_info = whois.whois(domain_name)
print(whois_info)