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

时间:2018-03-16 07:54:38

标签: python whois

代码

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)

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

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

此代码提供的输出

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.

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

2 个答案:

答案 0 :(得分:1)

您可以使用dict.get

<强>实施例

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

print (w)

答案 1 :(得分:0)

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

因此,您需要捕获此异常并处理它。

用以下内容替换循环的结尾:

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

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