-2

I'm really confused on how to bypass this problem from whois, is there a way so that when whois outputs the domain variable and it doesn't contain the specific variable that i want to showcase, it would just skip it instead of giving me an error?

Contains Emails in array

And below does not contain any web variable when whois outputs. Does not contain emails

This is the error that i got when inside the array list doesn't contain any emails variables.

Cell In [419], line 4, in whodata(host) 2 res = whois.whois(host) 3 print(res['registrar']) ----> 4 print(res['emails']) 5 print(res['country']) 6 print("---------")

KeyError: 'emails'

import whois

def whodata(host):
    res = whois.whois(host)
    print(res['registrar'])
    print(res['emails'])
    print(res['country'])
    print("---------")
    
test = ["minecraft.net", "google.com",'mob1lebn1-co.id.co.id']

for host in test:
    whodata(host)

1 Answers1

0

You can use an if-else condition or ternary operator to check whether the dictionary contains the given key or not.

import whois

def whodata(host):
    res = whois.whois(host)
    print(res['registrar'])
    print(res['emails'] if 'emails' in res else '')
    print(res['country'])
    print("---------")
    
test = ["minecraft.net", "google.com",'mob1lebn1-co.id.co.id']

for host in test:
    whodata(host)
Darshil Jani
  • 800
  • 2
  • 13