检查域是否已注册

Mil*_*ano 17 python dns

我正在尝试制作一个不会注册域名的脚本.我在使用Python 2.7.我已经读过该模块whois应该能够做到这一点,但我写的代码会引发错误.

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

for dom in domains:
    domain = whois.Domain(dom)
    print domain.registrar 
Run Code Online (Sandbox Code Playgroud)

这是错误:

  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
Run Code Online (Sandbox Code Playgroud)

你知道什么可能是错的吗?或者你能给我一个更好的解决方案吗?

编辑:我尝试了pythonwhois模块,但它也返回错误.

编辑2:根据这里的一个解决方案,在SO上,我试图使用pywhois,这个代码也引发了错误.

import pywhois
w = pywhois.whois('google.com')
w.expiration_date
Run Code Online (Sandbox Code Playgroud)

错误:

w = pywhois.whois('google.com')
AttributeError: 'module' object has no attribute 'whois'
Run Code Online (Sandbox Code Playgroud)

mar*_*dze 18

如果你赞成,可以使用pythonwhois

>>> 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'] 
Run Code Online (Sandbox Code Playgroud)

它返回一个字典

{'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'}
Run Code Online (Sandbox Code Playgroud)

编辑:我检查了你的whois 代码为我工作.

>>> 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.
Run Code Online (Sandbox Code Playgroud)

这个API使用的Unix/Linux的whois的shell命令,如图所示它在这里你不应该添加http://之前的域名.或者如果你有一个unix/linux机器测试这个:

$ 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 ...
Run Code Online (Sandbox Code Playgroud)

但是使用http(可能是因为http(s)只是一种协议类型,并且没有任何域名本身的实现)

$ whois http://google.com
No whois server is known for this kind of object.
Run Code Online (Sandbox Code Playgroud)


Mar*_*oma 7

安装

pip install pythonwhois
Run Code Online (Sandbox Code Playgroud)

您可能需要执行pip3 install pythonwhois --user或类似的操作。

代码

import pythonwhois


def is_registered(site):
    """Check if a domain has an WHOIS record."""
    details = pythonwhois.get_whois(site)
    return not details['raw'][0].startswith('No match for')


names = ['google', 'af35foobar90']
for name in names:
    site = '{}.com'.format(name)
    print('{}: {}'.format(site, is_registered(site)))
Run Code Online (Sandbox Code Playgroud)


Pad*_*ham 6

whois项目已移至github,您可以使用以下命令安装它pip install python-whois

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

print(whois(domains[0]))

{'updated_date': datetime.datetime(2014, 8, 14, 0, 0), 'status': ['clientDeleteProhibited http://www.icann.org/epp#clientDeleteProhibited', 'clientTransferProhibited http://www.icann.org/epp#clientTransferProhibited', 'clientUpdateProhibited http://www.icann.org/epp#clientUpdateProhibited'], 'name': None, 'dnssec': None, 'city': None, 'expiration_date': datetime.datetime(2015, 8, 13, 0, 0), 'zipcode': None, 'domain_name': 'EXAMPLE.COM', 'country': None, 'whois_server': ['whois.enetica.com.au', 'whois.godaddy.com', 'whois.domain.com', 'whois.iana.org'], 'state': None, 'registrar': ['ENETICA PTY LTD', 'GODADDY.COM, LLC', 'DOMAIN.COM, LLC', 'RESERVED-INTERNET ASSIGNED NUMBERS AUTHORITY'], 'referral_url': ['http://www.enetica.com.au', 'http://registrar.godaddy.com', 'http://www.domain.com', 'http://res-dom.iana.org'], 'address': None, 'name_servers': ['A.IANA-SERVERS.NET', 'B.IANA-SERVERS.NET'], 'org': None, 'creation_date': datetime.datetime(1995, 8, 14, 0, 0), 'emails': None}
Run Code Online (Sandbox Code Playgroud)


myi*_*lab 5

我在 Python 3 中遇到了 python-whois 问题,但 Python 2 使用以下代码对我来说很好用。

首先,我建议卸载您可能已安装的任何 whois 模块。python-whois (0.6.1) 和 whois (0.7) 都使用相同的import whois,这给我带来了一些额外的困惑。

接下来,通过命令提示符、终端等安装python-whois。

pip install python-whois

安装后,在您首选的 Python IDE 中输入以下代码。

"""
Python = 2.79
OS = Windows 10
IDE = PyCharm 4.5
PyPIPackage = python-whois 0.6.1
"""

import whois
url = 'example.com'
w = whois.whois(url)
print w
Run Code Online (Sandbox Code Playgroud)

结果是一本字典。

{
  "updated_date": "2015-08-14 00:00:00", 
  "status": [
    "clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited", 
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited", 
    "clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited"
  ], 
  "name": null, 
  "dnssec": null, 
  "city": null, 
  "expiration_date": "2016-08-13 00:00:00", 
...
...
...
  "address": null, 
  "name_servers": [
    "A.IANA-SERVERS.NET", 
    "B.IANA-SERVERS.NET"
  ], 
  "org": null, 
  "creation_date": "1995-08-14 00:00:00", 
  "emails": null
}
Run Code Online (Sandbox Code Playgroud)