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

yipeiwu_com6年前Python基础

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>

相关文章

Python转码问题的解决方法

比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作 s.decode('gbk').encode('utf-8′) 可是,在实际开发中,我发现,这种办法经常会出...

在python中bool函数的取值方法

bool是Boolean的缩写,只有真(True)和假(False)两种取值 bool函数只有一个参数,并根据这个参数的值返回真或者假。 1.当对数字使用bool函数时,0返回假(Fal...

python使用flask与js进行前后台交互的例子

flask与js进行前后台交互代码如下,后台给前端发数据: python部分: # -*- coding: utf-8 -*- from flask import Flask,jso...

在Python中合并字典模块ChainMap的隐藏坑【推荐】

在Python中合并字典模块ChainMap的隐藏坑【推荐】

在Python中,当我们有两个字典需要合并的时候,可以使用字典的 update 方法,例如: a = {'a': 1, 'b': 2} b = {'x': 3, 'y': 4} a....

Python中BeautifuSoup库的用法使用详解

BeautifulSoup简介 Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下: Beautiful Soup提供一些简单的、pytho...