Skip to content

Commit cff9e58

Browse files
committed
added getting domain info tutorial
1 parent 3206128 commit cff9e58

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
8181
- [How to Use Proxies to Anonymize your Browsing and Scraping using Python](https://www.thepythoncode.com/article/using-proxies-using-requests-in-python). ([code](web-scraping/using-proxies))
8282
- [How to Extract Script and CSS Files from Web Pages in Python](https://www.thepythoncode.com/article/extract-web-page-script-and-css-files-in-python). ([code](web-scraping/webpage-js-css-extractor))
8383
- [How to Extract and Submit Web Forms from a URL using Python](https://www.thepythoncode.com/article/extracting-and-submitting-web-page-forms-in-python). ([code](web-scraping/extract-and-fill-forms))
84+
- [How to Get Domain Name Information in Python](https://www.thepythoncode.com/article/extracting-domain-name-information-in-python). ([code](web-scraping/get-domain-info))
8485

8586
- ### [Python Standard Library](https://www.thepythoncode.com/topic/python-standard-library)
8687
- [How to Transfer Files in the Network using Sockets in Python](https://www.thepythoncode.com/article/send-receive-files-using-sockets-python). ([code](general/transfer-files/))
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [How to Get Domain Name Information in Python](https://www.thepythoncode.com/article/extracting-domain-name-information-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Use `validate_domains.py` code to validate domains
5+
- Use `get_domain_info.py` code to get various domain information from WHOIS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import whois
2+
from validate_domains import is_registered
3+
4+
domain_name = "google.com"
5+
if is_registered(domain_name):
6+
whois_info = whois.whois(domain_name)
7+
# print the registrar
8+
print("Domain registrar:", whois_info.registrar)
9+
# print the WHOIS server
10+
print("WHOIS server:", whois_info.whois_server)
11+
# get the creation time
12+
print("Domain creation date:", whois_info.creation_date)
13+
# get expiration date
14+
print("Expiration date:", whois_info.expiration_date)
15+
# print all other info
16+
print(whois_info)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-whois
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import whois # pip install python-whois
2+
3+
def is_registered(domain_name):
4+
"""
5+
A function that returns a boolean indicating
6+
whether a `domain_name` is registered
7+
"""
8+
try:
9+
w = whois.whois(domain_name)
10+
except Exception:
11+
return False
12+
else:
13+
return bool(w.domain_name)
14+
15+
16+
if __name__ == "__main__":
17+
# list of registered & non registered domains to test our function
18+
domains = [
19+
"thepythoncode.com",
20+
"google.com",
21+
"github.com",
22+
"unknownrandomdomain.com",
23+
"notregistered.co"
24+
]
25+
# iterate over domains
26+
for domain in domains:
27+
print(domain, "is registered" if is_registered(domain) else "is not registered")
28+

0 commit comments

Comments
 (0)