域名检测

时间:2019-04-29 18:58:17

标签: python list

我想使用此Python代码获取合法域名的过滤器。 我看不到任何问题的代码,并已咨询了许多网站。无法区分正确和错误的域名。

def getDomains():
    with open('domains.txt', 'r+') as f:
        for domainName in f.read().splitlines():
            domains.append(domainName)

def run():
    for dom in domains:
       if dom is not None and dom != '':
           details = pythonwhois.get_whois(dom)
           if str(details) is None:
               unavailable.append(dom)
           else:
               available.append(dom)

结果应类似于:

可用域

google.com youtube.com


不可用的域

xcdv345.hgt.com letstrythis12398.net

3 个答案:

答案 0 :(得分:1)

即使未注册域,pythonwhois.get_whois也不是None

尝试打印pythonwhois.get_whois('jjj876686.njerfjr')的结果,例如字段contacts始终存在(结果和str(result)与None不同)(请参阅{{3 }})

答案 1 :(得分:1)

if str(details) is None:行将始终为False,即使detailsNone

运行str(None)会给您字符串'None',它与值None不同:

str(None) is None  # False
None is None       # True

答案 2 :(得分:0)

检查id返回的dictionary中是否存在pythonwhois.get_whois()密钥

示例:

import pythonwhois

domains = ['google.com', 'i-n-v-a-l-i-d.com']

for dom in domains:
  if dom is not None and dom != '':
    details = pythonwhois.get_whois(dom)

    # Check if 'id' key exists in the 'details' dictionary
    # if the 'id' key exists the domain is already registerd
    if 'id' in details:
      print('{} available'.format(dom))
    else:
      print('{} unavailable'.format(dom))

输出:

google.com available
i-n-v-a-l-i-d.com unavailable