检查域名是否已注册 [英] Check whether domain is registered
问题描述
我正在尝试创建一个返回未注册域的脚本.我正在使用Python 2.7.我已经读过模块 whois
应该能够做到这一点,但是我编写的代码引发了一个错误.
I'm trying to make a script that returns unregistered domains. I'm working in Python 2.7. I've read that the module whois
should be able to do that but the code I've written raises an error.
代码:
import whois
domains = ['http://www.example.com']
for dom in domains:
domain = whois.Domain(dom)
print domain.registrar
错误:
domain = whois.Domain(dom)
File "C:\Python27\lib\site-packages\whois\_3_adjust.py", line 12, in __init__
self.name = data['domain_name'][0].strip().lower()
TypeError: string indices must be integers, not str
您知道什么地方可能出问题吗?还是可以给我一个更好的解决方案?
Have you any idea what could be wrong? Or can you give me a better solution?
我尝试了pythonwhois模块,但它也返回了一个错误.
I tried the pythonwhois module but it returns an error too.
根据一种解决方案,在SO上,我尝试使用 pywhois
,此代码也引发了错误.
According to one solution here, on SO, I've tried to use pywhois
, this code raises an error too.
import pywhois
w = pywhois.whois('google.com')
w.expiration_date
错误:
w = pywhois.whois('google.com')
AttributeError: 'module' object has no attribute 'whois'
推荐答案
使用pythonwhois,如果您愿意的话,可能是
with pythonwhois if you favor, it could be
>>> import pythonwhois # i'm using this http://cryto.net/pythonwhois
>>> domains = ['google.com', 'stackoverflow.com']
>>> for dom in domains:
... details = pythonwhois.get_whois(dom)
... print details['contacts']['registrant']
返回字典
{'city': u'Mountain View',
'fax': u'+1.6506188571',
'name': u'Dns Admin',
'state': u'CA',
'phone': u'+1.6502530000',
'street': u'Please contact contact- admin@google.com, 1600 Amphitheatre Parkway',
'country': u'US',
'postalcode': u'94043',
'organization': u'Google Inc.',
'email': u'dns-admin@google.com'}
{'city': u'New York',
'name': u'Sysadmin Team',
'state': u'NY',
'phone': u'+1.2122328280',
'street': u'1 Exchange Plaza , Floor 26',
'country': u'US',
'postalcode': u'10006',
'organization': u'Stack Exchange, Inc.',
'email': u'sysadmin-team@stackoverflow.com'}
我检查了您的 whois
这段代码对我有用.
edit: i checked your whois
this code worked for me.
>>> import whois
>>> domains = ['google.com', 'stackoverflow.com']
>>> for dom in domains:
... domain = whois.query(dom)
... print domain.name, domain.registrar
...
google.com MARKMONITOR INC.
stackoverflow.com NAME.COM, INC.
此api使用Unix/linux的whois shell命令,并显示此处您不应在域名之前添加 http://
.或者,如果您有unix/linux机器,请对此进行测试:
this api uses unix/linux's whois shell command and as it shown here you shouldn't add http://
before domain name. or if you have a unix/linux machine test this:
$ whois google.com
Whois Server Version 2.0
Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information ...
但是使用http(可能是因为http(s)只是一种协议类型,而域名本身没有任何实现)
but with http (it is maybe because of http(s) is a just a protocol type, and doesn't have any realiton with domain name itself)
$ whois http://google.com
No whois server is known for this kind of object.
这篇关于检查域名是否已注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!