由于公司有大量域名信息需要定期查看是否需要續(xù)期,前期都是人工操作比較耗時、耗力。所以衍生了這個小工具。
實現(xiàn)了查詢域名到期時間、并且將近7天內(nèi)到期的域名在Excel中標(biāo)紅,當(dāng)然你也可以添加短信提醒和郵件提醒
代碼步驟1、將域名粘貼到指定txt文件中比如:domain.txt
2、將指定txt文件中內(nèi)容讀取到list中# 批量讀取文件中的域名def read_file(filePath): with open(filePath, "r") as f: # 打開文件 data = f.readlines() # 讀取文件 return data3、通過某網(wǎng)站獲取域名到期時間# 通過某網(wǎng)站獲取域名到期時間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、將結(jié)果寫入Excel文件# 寫入Excel文件def write_excel(domain_list): # 創(chuàng)建一個新的文件 with xlsxwriter.Workbook('host_ip.xlsx') as workbook: # 添加一個工作表 worksheet = workbook.add_worksheet('域名信息') # 設(shè)置一個加粗的格式 bold = workbook.add_format({"bold": True}) # 分別設(shè)置一下 A 和 B 列的寬度 worksheet.set_column('A:A', 50) worksheet.set_column('B:B', 15) # 先把表格的抬頭寫上,并設(shè)置字體加粗 worksheet.write('A1', '域名', bold) worksheet.write('B1', '信息', bold) # 設(shè)置數(shù)據(jù)寫入文件的初始行和列的索引位置 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)運行結(jié)果:
6、完整代碼#!/usr/bin/env python# -*- coding: utf-8 -*-# Author:高效碼農(nóng)import pprintimport timeimport randomimport xlsxwriterfrom datetime import datetimeimport urllib.requestfrom lxml import etree# 批量讀取文件中的域名def read_file(filePath): with open(filePath, "r") as f: # 打開文件 data = f.readlines() # 讀取文件 return data# 通過某網(wǎng)站獲取域名到期時間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): # 創(chuàng)建一個新的文件 with xlsxwriter.Workbook('host_ip.xlsx') as workbook: # 添加一個工作表 worksheet = workbook.add_worksheet('域名信息') # 設(shè)置一個加粗的格式 bold = workbook.add_format({"bold": True}) # 分別設(shè)置一下 A 和 B 列的寬度 worksheet.set_column('A:A', 50) worksheet.set_column('B:B', 15) # 先把表格的抬頭寫上,并設(shè)置字體加粗 worksheet.write('A1', '域名', bold) worksheet.write('B1', '信息', bold) # 設(shè)置數(shù)據(jù)寫入文件的初始行和列的索引位置 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 += 1urls = read_file('domain.txt')urls_list = get_expiry_date(urls)write_excel(urls_list)掃描二維碼推送至手機(jī)訪問。
版權(quán)聲明:本文由信途科技轉(zhuǎn)載于網(wǎng)絡(luò),如有侵權(quán)聯(lián)系站長刪除。
轉(zhuǎn)載請注明出處http://www.quickersubmitter.com/xintu/8569.html