代码如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport requestsimport reimport xml.dom.minidom#获取所有kb号def get_dirname(): list4 = [] dir = 'D:\\python\\2017' list = os.listdir(dir) for list1 in list: list2 = dir+\ list3 = os.listdir(list2) list4.extend(list3) return list4#获取kb系统类型def get_system(list4): url = 'https://www.tulingxueyuan.cn/d/file/p/20230525/fptk5p4qxyi.aspx' + list4 content = requests.get(url).content data1 = re.findall('for (.*) \(KB\d{7} content) return data1#将微软的官方名称与xx名称相匹配 system_matching(i): dict = {'Windows 7':'enumSV_Windows_7', 'Windows 7 for x64-based Systems':'enumSV_Windows_7_X64', 'Windows Server 2008 R2 for x64-based Systems':'enumSV_Windows_2008_R2_X64, 'Windows Server 2008':'enumSV_Windows_2008', 'Windows Vista':'enumSV_WinVista', 'Windows Server 2008 for x64-based Systems':'enumSV_Windows_2008_X64', 'Windows Vista for x64-based Systems':'enumSV_WinVista_X64', 'Windows Server 2008 R2 for x64-based Systems':'enumSV_Windows_2008_R2_X64, 'Windows Server 2012 R2 for x64-based Systems':'enumSV_Windows_2012_R2_X64, 'Windows 8.1 for x64-based Systems':'enumSV_Windows__SP1_X64, 'Windows Server 2012 for x64-based Systems':'enumSV_Windows_2012_X64', 'Windows Embedded 8 Standard for x64-based Systems':'enumSV_Windows_8_X64', 'Windows 7 for x86-based Systems':'enumSV_Windows_7', 'Windows 8.1 for x86-based Systems':'enumSV_Windows___SP1, 'Windows 8.1':'enumSV_Windows___SP1, 'Windows 8':'enumSV_Windows_8', 'Windows 8 for x64-based Systems':'enumSV_Windows_8_X64', 'Windows Server 2003 for x64-based Systems':'enumSV_2003_X64', 'Windows XP for x64-based Systems':'enumSV_WinXp_X64', 'Windows Server 2003':'enumSV_2003', 'Windows XP':'enumSV_WinXp', 'Windows Server 2012 R2':'enumSV_Windows_2012_R2_X64, 'Windows Server 2008 R2 x64 Edition':'enumSV_Windows_2008_R2_X64, 'Windows Server 2003 x64 Edition':'enumSV_2003_X64', 'Windows Server 2008 x64 Edition':'enumSV_Windows_2008_X64', 'Windows Server 2012':'enumSV_Windows_2012_X64', 'Windows XP x64 Edition':'enumSV_WinXp_X64' } if i in dict: return dict[i] return None#获取替代关系def get_patch_relationship(kb_number): url = 'https://www.tulingxueyuan.cn/d/file/p/20230525/fptk5p4qxyi.aspx' + kb_number content = requests.get(url).content data = re.findall('goToDetails\("(.*)"\)', content) data4 = [] for i in data: url1 = 'https://www.tulingxueyuan.cn/d/file/p/20230525/2su3zis2syd.aspx' + i content1 = requests.get(url1).content data1 = re.findall("updateid=.*", content1) data2 = re.findall("KB\d{7}", content1) len1 = len(data1) data3 = data2[1:len1] data4.extend(data3) return data4#生成xml格式的文件def create_xml_files(Vuli):#在内存中创建空文档 doc = xml.dom.minidom.Document() root = doc.createElement('VulExpiration#创建一个根节点Vulexpiration对象 nodeVulList = doc.createElement('VulList') doc.appendChild(root)#将根节点添加到文档对象中 for i in Vuli : nodeVul = doc.createElement('Vul') #为叶节点name设置文本节点,用于显示文本内容 nodekbid = doc.createElement("kbid") nodekbid.appendChild(doc.createTextNode(str(i["kbid"]))) nodeVulType = doc.createElement("VulType") nodeVulType.appendChild(doc.createTextNode(str(i["VulType"]))) nodeByReplaceList = doc.createElement("ByReplaceList") nodeByKbid = doc.createElement("ByKbid") nodeByKbid.appendChild(doc.createTextNode(str(i["ByKbid"]))) nodeSystemList = doc.createElement("SystemList") nodeSystem = doc.createElement("System") nodeSystem.appendChild(doc.createTextNode(str(i["System"]))) #将每个叶节点添加到父节点Vullist中, #最后,在根节点Vulexpiration中添加Vulllist nodeVulList.appendChild(nodeVul) nodeVul.appendChild(nodekbid) nodeVul.appendChild(nodeVulType) nodeVul.appendChild(nodeByReplaceList) nodeVul.appendChild(nodeSystemList) nodeByReplaceList.appendChild(nodeByKbid) nodeSystemList.appendChild(nodeSystem) root.appendChild(nodeVulList) fp = open('D:\\python\\VulExpiration.xml', 'w') doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8") #开始写xml文档 fp.close() # def create_xml_format():if __name__ == '__main__': kb_number_list = get_dirname() for kb_number in kb_number_list: data1 = get_system(kb_number) data3 = get_patch_relationship(kb_number) if len(data3) == 0: continue for i in data1: systemvalue = system_matching(i) if systemvalue == None: continue Vuli = [{'kbid' : kb_number, 'VulType' : 1, 'ByKbid' : data3,System':systemvalue}] create_xml_files(Vuli)
数据似乎是错误的,但代码有一些可供参考的地方。