域名批量查詢,域名查詢工具 域名批量查詢系統



文章插圖
域名批量查詢,域名查詢工具 域名批量查詢系統

文章插圖
前言
由于公司有大量域名信息需要定期查看是否需要續期,前期都是人工操作比較耗時、耗力 。所以衍生了這個小工具 。
實現了查詢域名到期時間、并且將近7天內到期的域名在Excel中標紅,當然你也可以添加短信提醒和郵件提醒
代碼步驟1、將域名粘貼到指定txt文件中
比如:domain.txt
2、將指定txt文件中內容讀取到list中
# 批量讀取文件中的域名def read_file(filePath):with open(filePath, "r") as f:# 打開文件data = http://www.mnbkw.com/jxjc/176323/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'] = endTimepprint.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 = 1col = 0for 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 - currDateif 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 pprintimport timeimport randomimport xlsxwriterfrom datetime import datetimeimport urllib.requestfrom lxml import etree# 批量讀取文件中的域名def read_file(filePath):with open(filePath, "r") as f:# 打開文件data = http://www.mnbkw.com/jxjc/176323/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'] = endTimepprint.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 = 1col = 0for 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 - currDateif 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)