Python批量检测域名是否被注册

注册域名时会发现好名字基本都已被注册了,想找一个稍微好点的域名怎么办?

首先,根据自己的需要,按规则批量生成待检测域名。但是生成后可能会发现域名太多了,手动测试不现实,那么开始使用下面的代码自动检测吧!

#!/usr/bin/env python
# -*-coding:utf8-*-
 
from urllib import request
from multiprocessing.pool import Pool   #进程池管理包,Pool是其中的类

#未注册域名写入文件:mfile:文件名,message:未注册域名
def writelog(mfile, message):
    with open(mfile, 'a+') as f:        #a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
        f.write(message)                

#创建域名
def createdomain(sfx):
    with open('yuming.txt', 'r') as words:	#打开待检测域名文本
        for word in words:
            domain = word.strip() + sfx
            yield domain                #yield是一个类似return的关键字,yield返回一个生成器。 

#检测域名状态:domain:待检测域名
def checkdomainstatus(domain):
    API = "http://panda.www.net.cn/cgi-bin/check.cgi?area_domain="  
    url = API + domain                  #访问的URL链接,万网域名注册检测API接口
    try:
        xhtml = request.urlopen(url, timeout=5).read().decode() #打开网页,读取内容
    except Exception as e:              
        print(e)
        return
    r = xhtml.find(r'<original>210')    #字符串210表示还未被注册,字符串211表示已经被注册
    if r != -1:                         #如果包含子串,则返回子串开始的索引,否则返回-1
        print(domain + '尚未被注册')
        #调用writelog函数,将domain写入尚未被注册域名.txt文件中
        writelog('尚未被注册域名.txt', domain+'\n')   

if __name__ == "__main__":              
    print('开始检测...')
    suffix = '.com'                     #定义域名后缀变量的值为.com
    domains = createdomain(suffix)      #生成完整域名
    num = 5                             #定义进程数为5
    #创建进程池,如参数processes不设置,函数会跟根据计算机的实际情况来决定要运行多少个进程
    task_pool = Pool(processes=num)               
    #map()会将第二个参数的元素依次传入第一个参数中
    results = task_pool.map(checkdomainstatus, domains)  
    task_pool.close()                   #关闭进程池,之后不会有子进程加入
    task_pool.join()                    #等待所有子进程结束
    print('检测完成!')

即使使用了进程池,但整个过程也很慢,我检测的域名有166464个,耗费了1小时左右。

参考链接:https://github.com/JaesonCheng/register_domain/blob/master/register_domain.py

猜你喜欢

转载自blog.csdn.net/qq_38882327/article/details/90067263