由于公司有大量域名信息需要定期查看是否需要续期,前期都是人工操作比较耗时、耗力。所以衍生了这个小工具。
实现了查询域名到期时间、并且将近7天内到期的域名在Excel中标红,当然你也可以添加短信提醒和邮件提醒
代码步骤1、将域名粘贴到指定txt文件中比如:domain.txt
2、将指定txt文件中内容读取到list中#批量读取文件中的域名defread_file(filePath):withopen(filePath,"r")asf:#打开文件data=f.readlines()#读取文件returndata3、通过某网站获取域名到期时间#通过某网站获取域名到期时间defget_expiry_date(url_list):url_expiry_date_list=[]forurlinurl_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()')iflen(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)returnurl_expiry_date_list4、将结果写入Excel文件#写入Excel文件defwrite_excel(domain_list):#创建一个新的文件withxlsxwriter.Workbook('host_ip.xlsx')asworkbook:#添加一个工作表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=0fordomain_ex_dateindomain_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-currDateifdiffDate.days5、运行urls=read_file('domain.txt')urls_list=get_expiry_date(urls)write_excel(urls_list)运行结果:
6、完整代码#!/usr/bin/envpython#-*-coding:utf-8-*-#Author:高效码农importpprintimporttimeimportrandomimportxlsxwriterfromdatetimeimportdatetimeimporturllib.requestfromlxmlimportetree#批量读取文件中的域名defread_file(filePath):withopen(filePath,"r")asf:#打开文件data=f.readlines()#读取文件returndata#通过某网站获取域名到期时间defget_expiry_date(url_list):url_expiry_date_list=[]forurlinurl_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()')iflen(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)returnurl_expiry_date_list#写入Excel文件defwrite_excel(domain_list):#创建一个新的文件withxlsxwriter.Workbook('host_ip.xlsx')asworkbook:#添加一个工作表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=0fordomain_ex_dateindomain_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-currDateifdiffDate.days