In this article we will discuss how to get domain name information using Python.
Table of Contents
- Checking the domain name registration
- Getting domain name information
A domain name is a representation of an IP address of a resource. When you decide to visit https://pyshark.com/ you are going to an IP address of the website and the domain name here is just its identification string.
To get any domain name it needs to be purchases from a domain registration company. During the registration process of a domain name the registrants provide a lot of information like name, address, country, and more.
All of this information is stored and can be retrieved using WHOIS. It is a protocol that is widely used to get data from databases that store information about the domain name.
Let’s see how we can get domain name information using Python.
To continue following this tutorial we will need the following Python library: python-whois.
If you don’t have it installed, please open “Command Prompt” (on Windows) and install them using the following code:
pip install python-whois
Checking the domain name registration using Python
To get started we will first import the required library and create a domain variable and pass the URL we want to get information about:
import whois domain='www.pyshark.com'
The usage of the functions of the python-whois library is very simple. Now, we know that www.pyshark.com exists since you are on this website and are reading this article.
To get an object that contains the WHOIS information about this domain name we need to use the following code:
domain_info = whois.whois(domain)
Note that this code will only execute successfully if the domain name is registered. If it’s not it will give you an error.
We can use this information to build a function that will simply return True/False is the domain name is registered/not registered:
def check_reg(name): try: domain_info = whois.whois(name) return True except: return False
What this function will do is it will try to retrieve the WHOIS object with information about the domain name, and if it succeeds, it will return True. If not False which means that the domain name isn’t registered.
Let’s give it a try:
and you should get:
What this result tells us is that it is a registered domain. For us this means that we can retrieve some information about it.
Now if you tried running this function against some random domain that doesn’t exist, the function would return “False” which will mean that any further information retrieval of information is not possible simply because the domain isn’t registered.
Getting domain name information using Python
Now let’s look into how we can actually retrieve the registrar’s information from a valid domain name.
From the previous section we already learnt how to get an object containing the WHOIS information:
domain_info = whois.whois(domain)
What we get in return in a WHOIS object that we will work with like a dictionary.
Since we can work with it like a dictionary, we can get its keys to determine what information we have contained in it:
for key in domain_info.keys(): print(key)
And we get:
domain_name registrar whois_server referral_url updated_date creation_date expiration_date name_servers status emails dnssec name org address city state zipcode country
It’s quite a bit of available information and depending on which one you would like to retrieve, you can select the needed ones.
The final step is to print out the key-value pairs to have the actual information about our domain:
for key, value in domain_info.items(): print(key,':', value)
And we get:
domain_name : PYSHARK.COM registrar : FastDomain Inc. whois_server : whois.bluehost.com referral_url : None updated_date : [datetime.datetime(2020, 2, 4, 0, 39, 22), datetime.datetime(2020, 2, 4, 0, 39, 23)] creation_date : 2020-02-04 00:39:22 expiration_date : 2021-02-04 00:39:22 name_servers : ['NS1.BLUEHOST.COM', 'NS2.BLUEHOST.COM'] status : clientTransferProhibited https://icann.org/epp#clientTransferProhibited emails : ['[email protected]', '[email protected]'] dnssec : unsigned name : DOMAIN PRIVACY SERVICE FBO REGISTRANT org : THE ENDURANCE INTERNATIONAL GROUP, INC. address : 10 CORPORATE DR, STE 300 city : BURLINGTON state : MASSACHUSETTS zipcode : 01803 country : US
In this article we explored how to retrieve the domain name information using WHOIS.
This information is publicly available and when you purchase a domain name, you provide the information during registration which then is available and can be retrieved.
I also encourage you to check out my other posts on Python Programming.
Feel free to leave comments below if you have any questions or have suggestions for some edits.