由于公司有大量域名信息需要定期查看是否需要续期,前期都是人工操作比较耗时、耗力。所以衍生了这个小工具。
实现了查询域名到期时间、并且将近7天内到期的域名在Excel中标红,当然你也可以添加短信提醒和邮件提醒
比如:domain.txt
批量读取文件中的域名
def read_file(filePath):
with open(filePath, &34;r&34;) 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(&39;http://whois.xxxxxx.com/&39; + url)
result = req_whois.read().decode()
html = etree.HTML(result)
endTimes = html.xpath(&39;//a[@id=&34;update_a2&34;]/preceding-sibling::span[1]/text()&39;)
if len(endTimes) > 0:
endTime = endTimes[0].replace(&39;年&39;, &39;-&39;).replace(&39;月&39;, &39;-&39;).replace(&39;日&39;, &39;&39;)
else:
errorInfo = html.xpath(&39;//div[@class=&34;IcpMain02&34;]&39;)
endTime = errorInfo[0].xpath(&39;string(.)&39;).strip()
url_expiry_date_dict[&39;url&39;] = url.replace(&39;n&39;, &39;&39;)
url_expiry_date_dict[&39;endTime&39;] = 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(&39;host_ip.xlsx&39;) as workbook:
添加一个工作表
worksheet = workbook.add_worksheet(&39;域名信息&39;)
设置一个加粗的格式
bold = workbook.add_format({&34;bold&34;: True})
分别设置一下 A 和 B 列的宽度
worksheet.set_column(&39;A:A&39;, 50)
worksheet.set_column(&39;B:B&39;, 15)
先把表格的抬头写上,并设置字体加粗
worksheet.write(&39;A1&39;, &39;域名&39;, bold)
worksheet.write(&39;B1&39;, &39;信息&39;, bold)
设置数据写入文件的初始行和列的索引位置
row = 1
col = 0
for domain_ex_date in domain_list:
url = domain_ex_date[&39;url&39;]
endTime = domain_ex_date[&39;endTime&39;]
currDate = datetime.today().date()
try:
endDate = datetime.strptime(endTime, &34;%Y-%m-%d&34;).date()
diffDate = endDate - currDate
if diffDate.days <= 7:
style = workbook.add_format({&39;font_color&39;: &34;red&34;})
else:
style = workbook.add_format({&39;font_color&39;: &34;black&34;})
except:
style = workbook.add_format({&39;font_color&39;: &34;red&34;})
pprint.pprint(url + &39;: &39; + endTime)
worksheet.write(row, col, url, style)
worksheet.write(row, col + 1, endTime, style)
row += 1
urls = read_file(&39;domain.txt&39;)
urls_list = get_expiry_date(urls)
write_excel(urls_list)
运行结果:
!/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, &34;r&34;) 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(&39;http://whois.xxxxxx.com/&39; + url)
result = req_whois.read().decode()
html = etree.HTML(result)
endTimes = html.xpath(&39;//a[@id=&34;update_a2&34;]/preceding-sibling::span[1]/text()&39;)
if len(endTimes) > 0:
endTime = endTimes[0].replace(&39;年&39;, &39;-&39;).replace(&39;月&39;, &39;-&39;).replace(&39;日&39;, &39;&39;)
else:
errorInfo = html.xpath(&39;//div[@class=&34;IcpMain02&34;]&39;)
endTime = errorInfo[0].xpath(&39;string(.)&39;).strip()
url_expiry_date_dict[&39;url&39;] = url.replace(&39;n&39;, &39;&39;)
url_expiry_date_dict[&39;endTime&39;] = 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(&39;host_ip.xlsx&39;) as workbook:
添加一个工作表
worksheet = workbook.add_worksheet(&39;域名信息&39;)
设置一个加粗的格式
bold = workbook.add_format({&34;bold&34;: True})
分别设置一下 A 和 B 列的宽度
worksheet.set_column(&39;A:A&39;, 50)
worksheet.set_column(&39;B:B&39;, 15)
先把表格的抬头写上,并设置字体加粗
worksheet.write(&39;A1&39;, &39;域名&39;, bold)
worksheet.write(&39;B1&39;, &39;信息&39;, bold)
设置数据写入文件的初始行和列的索引位置
row = 1
col = 0
for domain_ex_date in domain_list:
url = domain_ex_date[&39;url&39;]
endTime = domain_ex_date[&39;endTime&39;]
currDate = datetime.today().date()
try:
endDate = datetime.strptime(endTime, &34;%Y-%m-%d&34;).date()
diffDate = endDate - currDate
if diffDate.days <= 7:
style = workbook.add_format({&39;font_color&39;: &34;red&34;})
else:
style = workbook.add_format({&39;font_color&39;: &34;black&34;})
except:
style = workbook.add_format({&39;font_color&39;: &34;red&34;})
pprint.pprint(url + &39;: &39; + endTime)
worksheet.write(row, col, url, style)
worksheet.write(row, col + 1, endTime, style)
row += 1
urls = read_file(&39;domain.txt&39;)
urls_list = get_expiry_date(urls)
write_excel(urls_list)