我正在尝试制作一个返回未注册域的脚本。我在Python2.7工作。我已经读到模块whois应该能够做到这一点,但是我编写的代码会引发一个错误。
代码:
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你知道会出什么问题吗?或者你能给我一个更好的解决方案吗?
编辑:我尝试了pythonwhois模块,但它也返回了一个错误。
EDIT2:根据这里的一个解决方案,因此,我尝试使用pywhois,这段代码也会引发一个错误。
import pywhois
w = pywhois.whois('google.com')
w.expiration_date错误:
w = pywhois.whois('google.com')
AttributeError: 'module' object has no attribute 'whois'发布于 2015-04-21 13:28:37
如果你喜欢的话,它可能是
>>> 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,这段代码对我有用。
>>> 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命令,正如它所显示的,这里不应该在域名之前添加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 ...但是对于http (可能是因为http(s)只是一种协议类型,并且与域名本身没有任何关系)。
$ whois http://google.com
No whois server is known for this kind of object.发布于 2016-05-08 21:42:39
我在Python 3中遇到了Python的问题,但是Python 2使用下面的代码对我来说很好。
首先,我建议卸载您可能已经安装的任何whois模块。python (0.6.1)和whois (0.7)都使用相同的import whois,这给我带来了一些额外的困惑。
接下来,通过命令提示符、终端等安装python。
pip install python-whois
安装完毕后,在首选python中输入以下代码。
"""
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结果就是一本字典。
{
"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
}发布于 2019-05-30 06:42:46
安装
pip install pythonwhois您可能需要执行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)))https://stackoverflow.com/questions/29773003
复制相似问题