Python 检查域是否已注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29773003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether domain is registered
提问by Milano
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.
我正在尝试制作一个返回未注册域的脚本。我正在使用 Python 2.7。我读过该模块whois
应该能够做到这一点,但我编写的代码引发了错误。
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:\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?
你知道什么可能是错的吗?或者你能给我一个更好的解决方案吗?
EDIT: I tried the pythonwhois module but it returns an error too.
编辑:我尝试了 pythonwhois 模块,但它也返回错误。
EDIT2: According to one solution here, on SO, I've tried to use pywhois
, this code raises an error too.
EDIT2:根据此处的一个解决方案,在 SO 上,我尝试使用pywhois
,此代码也会引发错误。
import pywhois
w = pywhois.whois('google.com')
w.expiration_date
ERROR:
错误:
w = pywhois.whois('google.com')
AttributeError: 'module' object has no attribute 'whois'
采纳答案by marmeladze
with pythonwhois if you favor, it could be
如果您愿意,可以使用 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']
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- [email protected], 1600 Amphitheatre Parkway',
'country': u'US',
'postalcode': u'94043',
'organization': u'Google Inc.',
'email': u'[email protected]'}
{'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'[email protected]'}
edit: i checked your whois
this code worked for me.
编辑:我检查了你的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.
this api uses unix/linux's whois shell command and as it shown hereyou shouldn't add http://
before domain name. or if you have a unix/linux machine test this:
这个 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 ...
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)
但是使用http(这可能是因为http(s)只是一种协议类型,并且与域名本身没有任何关系)
$ whois http://google.com
No whois server is known for this kind of object.
回答by Ajay
I've checked the documentationand worked for me.Domain name should be like mysite.com(not http://www.example.com)
我检查了文档并为我工作。域名应该像 mysite.com(不是http://www.example.com)
>>> import whois
>>> domains = ['google.com']
>>>
>>> for dom in domains:
... domain = whois.query(dom)
... print domain.registrar
...
MARKMONITOR INC.
EDIT:1 I don't know why it is not working for others and getting errors.
I'm posting a screenshot of my terminal
编辑:1 我不知道为什么它对其他人不起作用并出现错误。我正在发布我的终端的屏幕截图
回答by Padraic Cunningham
The whois project has been moved to github, you can install it using pip install python-whois
:
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}
回答by datalifenyc
I've had issues with python-whois in Python 3, but Python 2 works fine for me using the following code.
我在 Python 3 中遇到了 python-whois 问题,但 Python 2 使用以下代码对我来说很好用。
First, I would recommend uninstalling any whois module(s) you might have installed. Both python-whois (0.6.1) and whois (0.7) use the same import whois
, which created some additional confusion for me.
首先,我建议卸载您可能已安装的任何 whois 模块。python-whois (0.6.1) 和 whois (0.7) 都使用相同的import whois
,这给我带来了一些额外的困惑。
Next, install python-whois through Command Prompt, Terminal, etc.
接下来,通过命令提示符、终端等安装python-whois。
pip install python-whois
pip install python-whois
Once installed, enter the following code in your preferred python IDE.
安装后,在您首选的 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
The result is a dictionary.
结果是一本字典。
{
"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
}
回答by Martin Thoma
Installation
安装
pip install pythonwhois
You might need to execute pip3 install pythonwhois --user
or something similar.
您可能需要执行pip3 install pythonwhois --user
或类似的操作。
Code
代码
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)))