欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  新闻

过期域名批量查询工具(域名状态查询工具)

程序员文章站 2024-03-26 19:00:56
前言由于公司有大量域名信息需要定期查看是否需要续期,前期都是人工操作比较耗时、耗力。所以衍生了这个小工具。实现了查询域名到期时间、并且将近7天内到期的域名在excel中标红,当然你也可以添加短信提醒和...

过期域名批量查询工具(域名状态查询工具)

前言

由于公司有大量域名信息需要定期查看是否需要续期,前期都是人工操作比较耗时、耗力。所以衍生了这个小工具。

实现了查询域名到期时间、并且将近7天内到期的域名在excel中标红,当然你也可以添加短信提醒和邮件提醒

代码步骤

1、将域名粘贴到指定txt文件中

比如:domain.txt

过期域名批量查询工具(域名状态查询工具)

2、将指定txt文件中内容读取到list中

# 批量读取文件中的域名
def read_file(filepath):
    with open(filepath, "r") as f:  # 打开文件
        data = f.readlines()  # 读取文件
        return data

3、通过某网站获取域名到期时间

# 通过某网站获取域名到期时间
def get_expiry_date(url_list):
    url_expiry_date_list = []
    for url in url_list:
        url_expiry_date_dict = {}
        time.sleep(random.randrange(3))
        req_whois = urllib.request.urlopen('http://whois.xxxxxx.com/' + url)
        result = req_whois.read().decode()
        html = etree.html(result)

        endtimes = html.xpath('//a[@id="update_a2"]/preceding-sibling::span[1]/text()')
        if len(endtimes) > 0:
            endtime = endtimes[0].replace('年', '-').replace('月', '-').replace('日', '')
        else:
            errorinfo = html.xpath('//div[@class="icpmain02"]')
            endtime = errorinfo[0].xpath('string(.)').strip()
        url_expiry_date_dict['url'] = url.replace('n', '')
        url_expiry_date_dict['endtime'] = endtime
        pprint.pprint(url_expiry_date_dict)
        url_expiry_date_list.append(url_expiry_date_dict)
    pprint.pprint(url_expiry_date_list)
    return url_expiry_date_list

4、将结果写入excel文件

# 写入excel文件
def write_excel(domain_list):
    # 创建一个新的文件
    with xlsxwriter.workbook('host_ip.xlsx') as workbook:
        # 添加一个工作表
        worksheet = workbook.add_worksheet('域名信息')
        # 设置一个加粗的格式
        bold = workbook.add_format({"bold": true})
        # 分别设置一下 a 和 b 列的宽度
        worksheet.set_column('a:a', 50)
        worksheet.set_column('b:b', 15)
        # 先把表格的抬头写上,并设置字体加粗
        worksheet.write('a1', '域名', bold)
        worksheet.write('b1', '信息', bold)
        # 设置数据写入文件的初始行和列的索引位置
        row = 1
        col = 0
        for domain_ex_date in domain_list:
            url = domain_ex_date['url']
            endtime = domain_ex_date['endtime']
            currdate = datetime.today().date()
            try:
                enddate = datetime.strptime(endtime, "%y-%m-%d").date()
                diffdate = enddate - currdate
                if diffdate.days <= 7:
                    style = workbook.add_format({'font_color': "red"})
                else:
                    style = workbook.add_format({'font_color': "black"})
            except:
                style = workbook.add_format({'font_color': "red"})
            pprint.pprint(url + ': ' + endtime)
            worksheet.write(row, col, url, style)
            worksheet.write(row, col + 1, endtime, style)
            row += 1

5、运行

urls = read_file('domain.txt')
urls_list = get_expiry_date(urls)
write_excel(urls_list)

运行结果:

过期域名批量查询工具(域名状态查询工具)

6、完整代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:高效码农

import pprint
import time
import random
import xlsxwriter
from datetime import datetime
import urllib.request
from lxml import etree


# 批量读取文件中的域名
def read_file(filepath):
    with open(filepath, "r") as f:  # 打开文件
        data = f.readlines()  # 读取文件
        return data


# 通过某网站获取域名到期时间
def get_expiry_date(url_list):
    url_expiry_date_list = []
    for url in url_list:
        url_expiry_date_dict = {}
        time.sleep(random.randrange(3))
        req_whois = urllib.request.urlopen('http://whois.xxxxxx.com/' + url)
        result = req_whois.read().decode()
        html = etree.html(result)

        endtimes = html.xpath('//a[@id="update_a2"]/preceding-sibling::span[1]/text()')
        if len(endtimes) > 0:
            endtime = endtimes[0].replace('年', '-').replace('月', '-').replace('日', '')
        else:
            errorinfo = html.xpath('//div[@class="icpmain02"]')
            endtime = errorinfo[0].xpath('string(.)').strip()
        url_expiry_date_dict['url'] = url.replace('n', '')
        url_expiry_date_dict['endtime'] = endtime
        pprint.pprint(url_expiry_date_dict)
        url_expiry_date_list.append(url_expiry_date_dict)
    pprint.pprint(url_expiry_date_list)
    return url_expiry_date_list


# 写入excel文件
def write_excel(domain_list):
    # 创建一个新的文件
    with xlsxwriter.workbook('host_ip.xlsx') as workbook:
        # 添加一个工作表
        worksheet = workbook.add_worksheet('域名信息')
        # 设置一个加粗的格式
        bold = workbook.add_format({"bold": true})
        # 分别设置一下 a 和 b 列的宽度
        worksheet.set_column('a:a', 50)
        worksheet.set_column('b:b', 15)
        # 先把表格的抬头写上,并设置字体加粗
        worksheet.write('a1', '域名', bold)
        worksheet.write('b1', '信息', bold)
        # 设置数据写入文件的初始行和列的索引位置
        row = 1
        col = 0
        for domain_ex_date in domain_list:
            url = domain_ex_date['url']
            endtime = domain_ex_date['endtime']
            currdate = datetime.today().date()
            try:
                enddate = datetime.strptime(endtime, "%y-%m-%d").date()
                diffdate = enddate - currdate
                if diffdate.days <= 7:
                    style = workbook.add_format({'font_color': "red"})
                else:
                    style = workbook.add_format({'font_color': "black"})
            except:
                style = workbook.add_format({'font_color': "red"})
            pprint.pprint(url + ': ' + endtime)
            worksheet.write(row, col, url, style)
            worksheet.write(row, col + 1, endtime, style)
            row += 1


urls = read_file('domain.txt')
urls_list = get_expiry_date(urls)
write_excel(urls_list)