在 python 中找不到任何内容或 whois 无法获得结果时如何存储默认值

How to store default value when nothing is found or whois fails to get results in python

代码

import whois 
domains = ['google.com', 'whosebug.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)

预计存储 w = ['GOOGLE.COM', 'whosebug.com', 'none', 'facebook.com' ]

错误是它找不到域并且它停止检查下一个域

这段代码给出的输出

google.com
whosebug.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.

如何消除此错误或添加一些默认值(如 none 并将其存储在列表中,以及当出现此错误或找不到域时继续检查其余域。

您可以使用dict.get

例如:

import whois 
domains = ['google.com', 'whosebug.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)

当找不到域名时,您使用的库会产生异常(whois.parser.PywhoisError)。

因此,您需要捕获这个异常并进行处理。

用类似的东西替换循环的结尾:

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

您可能需要在文件顶部添加一个 import whois.parser,以便在您的程序中识别异常(已解析符号)。