Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

python - Check whether domain is registered

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.

Code:

import whois
domains = ['http://www.example.com']

for dom in domains:
    domain = whois.Domain(dom)
    print domain.registrar 

Error:

  domain = whois.Domain(dom)
  File "C:Python27libsite-packageswhois\_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?

EDIT: I tried the pythonwhois module but it returns an error too.

EDIT2: 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

ERROR:

w = pywhois.whois('google.com')
AttributeError: 'module' object has no attribute 'whois'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'] 

which returns a dictionary

{'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'}

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.

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 ...

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...