1

The code

import whois 
domains = ['google.com', 'stackoverflow.com', 'hdtrcs.com' , 'facebook.com' ]
w = []
i = 0
for data in domains:
    n = domains[i]
    print(n)
    i = i+1
    data = whois.whois(n)
    if data != None:
            w.append(data['domain_name'])
    else:
        w.append('none')
print (w)

Expected to store w = ['GOOGLE.COM', 'STACKOVERFLOW.COM', 'none', 'facebook.com' ]

The error is it cant find the domain and it stops checking next domains

The output this code gives

google.com
stackoverflow.com
hdtrcs.com
Traceback (most recent call last):
  File "who.py", line 9, in <module>
    data = whois.whois(n)
  File "/usr/local/lib/python3.5/dist-packages/whois/__init__.py", line 41, in whois
    return WhoisEntry.load(domain, text)
  File "/usr/local/lib/python3.5/dist-packages/whois/parser.py", line 185, in load
    return WhoisCom(domain, text)
  File "/usr/local/lib/python3.5/dist-packages/whois/parser.py", line 283, in __init__
    raise PywhoisError(text)
whois.parser.PywhoisError: No match for "HDTRCS.COM".
>>> Last update of whois database: 2018-03-16T09:41:06Z <<<

NOTICE: The expiration date displayed in this record is the date the
registrar's sponsorship of the domain name registration in the registry is
currently set to expire. This date does not necessarily reflect the expiration
date of the domain name registrant's agreement with the sponsoring
registrar.  Users may consult the sponsoring registrar's Whois database to
view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois
database through the use of electronic processes that are high-volume and
automated except as reasonably necessary to register domain names or
modify existing registrations; the Data in VeriSign Global Registry
Services' ("VeriSign") Whois database is provided by VeriSign for
information purposes only, and to assist persons in obtaining information
about or related to a domain name registration record. VeriSign does not
guarantee its accuracy. By submitting a Whois query, you agree to abide
by the following terms of use: You agree that you may use this Data only
for lawful purposes and that under no circumstances will you use this Data
to: (1) allow, enable, or otherwise support the transmission of mass
unsolicited, commercial advertising or solicitations via e-mail, telephone,
or facsimile; or (2) enable high volume, automated, electronic processes
that apply to VeriSign (or its computer systems). The compilation,
repackaging, dissemination or other use of this Data is expressly
prohibited without the prior written consent of VeriSign. You agree not to
use electronic processes that are automated and high-volume to access or
query the Whois database except as reasonably necessary to register
domain names or modify existing registrations. VeriSign reserves the right
to restrict your access to the Whois database in its sole discretion to ensure
operational stability.  VeriSign may restrict or terminate your access to the
Whois database for failure to abide by these terms of use. VeriSign
reserves the right to modify these terms at any time.

The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.

How to get rid of this error or to add some default value like none and store it in the list and when this error arises or domain not found and continue to check for rest of the domains.

vipul-rao
  • 387
  • 1
  • 5
  • 12

2 Answers2

3

The library you use generates an exception (whois.parser.PywhoisError) when the domain name is not found.

Hence, you need to capture this exception and deal with it.

Replace the end of your loop by something like:

try:
    data = whois.whois(n)
    w.append(data['domain_name'])
except whois.parser.PywhoisError:
    w.append('none')

You may need an import whois.parser at top of your file, so that the exception is known (symbol resolved) in your program.

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
1

You can use dict.get

Ex:

import whois 
domains = ['google.com', 'stackoverflow.com', 'hdtrcs.com' , 'facebook.com' ]
result = []
for dom in domains:
    w = whois.whois(dom)
    if w:
        result.append(w.get('domain_name', 'none'))    

print (result)
Rakesh
  • 81,458
  • 17
  • 76
  • 113