首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中找不到任何东西或没有得到结果时,如何存储默认值

在python中找不到任何东西或没有得到结果时,如何存储默认值
EN

Stack Overflow用户
提问于 2018-03-16 07:54:37
回答 2查看 1.1K关注 0票数 1

代码

代码语言:javascript
复制
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' ]

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

这段代码给的输出

代码语言:javascript
复制
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.

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-17 15:50:28

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

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

将循环的末尾替换为如下所示:

代码语言:javascript
复制
try:
    data = whois.whois(n)
    w.append(data['domain_name'])
except whois.parser.PywhoisError:
    w.append('none')

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

票数 2
EN

Stack Overflow用户

发布于 2018-03-16 07:57:27

您可以使用dict.get

Ex:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49315620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档