侧边栏壁纸
  • 累计撰写 208 篇文章
  • 累计创建 16 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

使用Python脚本批量检测域名是否被注册

Wake
2024-12-11 / 0 评论 / 0 点赞 / 568 阅读 / 465 字

前言:

这个脚本可以简单查一下你想了解的域名是否有被注册,省去了一个个去查的时间。不要也不支持大量的查询,调用多了访问接口会被限制。后面就只能查到哪些域名是被注册了的。不过这个反推一下也是可以的,排除掉提示显示已注册的域名,得到那些没有结果的就是没有被注册的了。
脚本如下:(search_domains.py)

import whois
import traceback
from concurrent.futures import ThreadPoolExecutor

# 写入未注册域名或日志
def writelog(mfile, message):
    with open(mfile, 'a+', encoding='utf-8') as f:
        f.write(message)

# 生成待检测域名
def createdomain(suffix):
    with open(/home/ubuntu/domains.txt', 'r', encoding='utf-8') as words:
        for word in words:
            domain = word.strip() + suffix
            yield domain

# 检测域名状态
def checkdomainstatus(domain):
    try:
        # 使用 whois 查询域名
        w = whois.whois(domain)
        if w.status is None:
            print(f"{domain} 尚未被注册")
            writelog('available_domain.txt', domain + '\n')
        else:
            print(f"{domain} 已被注册")
    except Exception as e:
        # 捕获异常并写入日志
        error_message = f"Error checking {domain}: {e}\n{traceback.format_exc()}"
        print(error_message)
        writelog('error_log.txt', f"{domain} 检测失败: {e}\n")

# 主函数
def main():
    print('开始检测...')
    suffix = '.com'  # 定义域名后缀
    domains = createdomain(suffix)  # 生成完整域名

    num = 1  # 设置线程数
    with ThreadPoolExecutor(max_workers=num) as executor:
        executor.map(checkdomainstatus, domains)  # 并发检测域名状态

    print('检测完成!')

if __name__ == "__main__":
    main()

输出结果如下:
image-1733903980580
因为调用太频繁,接口被限制了,所以输出不了未被注册的信息,报错了。等到那边解除限制就可以正常使用了。结果还会输出脚本当前目录的available_domain.txt文件上。

0

评论区