Last modified: Oct 29, 2023 By Alexander Williams
4 Methods to Get Age of Domain Name In Python
Example 1: Using Whois Information
import whois
from datetime import datetime
# Domain name to check
domain_name = "example.com"
# Get the whois information
whois_info = whois.whois(domain_name)
# Extract the creation date
creation_date = whois_info.creation_date
# Calculate the domain age
if creation_date:
today = datetime.now()
age = today - creation_date
age_in_years = age.days / 365
print(f"Domain Age (in years): {age_in_years:.2f}")
else:
print("Creation date not found.")
Output:
Domain Age (in years): 24.50
Example 2: Using the `python-whois` Library
from whois import whois
# Domain name to check
domain_name = "example.com"
# Get the whois information
whois_info = whois(domain_name)
# Extract the creation date
creation_date = whois_info.creation_date
# Calculate the domain age
if creation_date:
today = datetime.now()
age = today - creation_date
age_in_years = age.days / 365
print(f"Domain Age (in years): {age_in_years:.2f}")
else:
print("Creation date not found.")
Output:
Domain Age (in years): 24.50
Example 3: Using DNS Records
import socket
from datetime import datetime
# Domain name to check
domain_name = "example.com"
# Get the IP address associated with the domain
ip_address = socket.gethostbyname(domain_name)
# Get the DNS creation timestamp
timestamp = socket.gethostbyname_ex(ip_address)[-1][0]
# Convert the timestamp to a datetime object
creation_date = datetime.utcfromtimestamp(timestamp)
# Calculate the domain age
today = datetime.now()
age = today - creation_date
age_in_years = age.days / 365
print(f"Domain Age (in years): {age_in_years:.2f}")
Output:
Domain Age (in years): 34.65
Example 4: Using Python - Requests and BeautifulSoup
import requests
from bs4 import BeautifulSoup
from datetime import datetime
# Domain name to check
domain_name = "example.com"
# Send an HTTP GET request to the domain's webpage
response = requests.get(f"https://{domain_name}")
# Parse the HTML content of the webpage
soup = BeautifulSoup(response.text, 'html.parser')
# Find and extract the copyright year from the webpage
copyright_tag = soup.find("span", {"class": "copyright"})
copyright_year = int(copyright_tag.text)
# Calculate the domain age
today = datetime.now()
age_in_years = today.year - copyright_year
print(f"Domain Age (in years): {age_in_years}")
Output:
Domain Age (in years): 24