我想使用此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
答案 0 :(得分:1)
即使未注册域,pythonwhois.get_whois
也不是None
。
尝试打印pythonwhois.get_whois('jjj876686.njerfjr')
的结果,例如字段contacts
始终存在(结果和str(result)与None
不同)(请参阅{{3 }})
答案 1 :(得分:1)
if str(details) is None:
行将始终为False
,即使details
为None
。
运行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