
前言
由於公司有大量域名信息需要定期查看是否需要續期,前期都是人工操作比較耗時、耗力。所以衍生了這個小工具。
實現了查詢域名到期時間、並且將近7天內到期的域名在Excel中標紅,當然你也可以添加短訊提醒和郵件提醒
代碼步驟
1、將域名粘貼到指定txt文件中
比如:domain.txt

2、將指定txt文件中內容讀取到list中
# 批量讀取文件中的域名
def read_file(filePath):
with open(filePath, "r") as f: # 打開文件
data = f.readlines() # 讀取文件
return data3、通過某網站獲取域名到期時間
# 通過某網站獲取域名到期時間
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_list4、將結果寫入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 += 15、運行
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)原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/255436.html
微信掃一掃
支付寶掃一掃