python3实现域名查询和whois查询功能

时间:2019-04-20
本文章向大家介绍python3实现域名查询和whois查询功能,主要包括python3实现域名查询和whois查询功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 域名查询

万网提供了域名查询接口,接口采用HTTP协议:

接口URL:http://panda.www.net.cn/cgi-bin/check.cgi

接口参数:area_domain,接口参数值为标准域名,例:doucube.com

调用举例:

http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=doucube.com

返回:

<?xml version="1.0" encoding="gb2312" ?> 
- <property>
 <returncode>200</returncode> 
 <key>doucube.com</key> 
 <original>211 : Domain name is not available</original> 
 </property>

返回结果说明:

<returncode>200</returncode> 返回码,200表示返回成功
<key>doucube.com</key> 表示当前查询的域名
<original>211 : Domain name is not available</original> 返回结果的原始信息,主要有以下几种

original=210 : Domain name is available  表示域名可以注册
original=211 : Domain name is not available 表示域名已经注册
original=212 : Domain name is invalid  表示查询的域名无效
original=213 : Time out 查询超时

用python3实现如下

1.1 查询已经被注册的域名

import urllib.request
req = urllib.request.urlopen('http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=doucube.com')
print(req.read().decode())

返回结果:不可用,已经被注册

<?xml version="1.0" encoding="gb2312" ?> 
- <property>
 <returncode>200</returncode> 
 <key>doucube.com</key> 
 <original>211 : Domain name is not available</original> 
 </property>

1.2 查询没有被注册的域名

req2 = urllib.request.urlopen('http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=doucube.net')
print(req2.read().decode())

返回结果:可用,未被注册

 <?xml version="1.0" encoding="gb2312" ?> 
- <property>
 <returncode>200</returncode> 
 <key>doucube.net</key> 
 <original>210 : Domain name is available</original> 
 </property>

1.3 查询不存在的域名,使用不存在的后缀

req3 = urllib.request.urlopen('http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=doucube.net2')
print(req3.read().decode())

返回结果:域名无效

 <?xml version="1.0" encoding="gb2312" ?> 
- <property>
 <returncode>200</returncode> 
 <key>doucube.net2</key> 
 <original>212 : Domain name is invalid</original> 
 </property>

.whois查询

由于没有找到像域名查询接口那样好的API,这里直接抓取站长之家的whois查询页面(http://whois.chinaz.com/)

req_whois = urllib.request.urlopen('http://whois.chinaz.com/doucube.com')
print(req_whois.read().decode())

在返回的结果中有这样一段html代码,这段信息就是查询的whois信息

<div style=" text-align:center;"> 
 <div class="div_whois">
  域名:doucube.com  
  <a href='http://www.doucube.com' target=_blank>访问此网站</a></div>
 <div id="whoisinfo" class="div_whois">
  注册商:GODADDY.COM, LLC<br/>
  域名服务器:whois.godaddy.com<br/>
  DNS服务器:DNS1.FREEHOSTIA.COM<br/>
  DNS服务器:DNS2.FREEHOSTIA.COM<br/>
  域名状态:运营商设置了客户禁止删除保护<br/>
  域名状态:运营商设置了客户禁止续费保护<br/>
  域名状态:运营商设置了客户禁止转移保护<br/>
  域名状态:运营商设置了客户禁止修改保护<br/>
  更新时间:2012年05月28日<br/>
  创建时间:2012年05月23日<br/>
  过期时间:2013年05月23日<br/>
  联系人:zhu, alice<br/>
  联系方式:<img src="/displayemail.aspx?email=M8N8oc1O|iQhqGCDHdpH9m77v2qrQfW8"/>
  <br/>
  <br/>
 </div>
</div>