代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import requests
import re
import xml.dom.minidom


#获取所有kb号
def get_dirname():
    list4 = []
    dir = 'D:\\python\\2017'
    list = os.listdir(dir)
    for list1 in list:
        list2 = dir+'\\'+list1
        list3 = os.listdir(list2)
        list4.extend(list3)
    return list4

#获取kb的系统类型
def get_system(list4):
        url = 'http://www.catalog.update.microsoft.com/Search.aspx?q=' + list4
        content = requests.get(url).content
        data1 = re.findall('for (.*) \(KB\d{7}\)', content)
        return data1

#将微软官方名称与xx名称相匹配
def 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_8_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_8_SP1',
               'Windows 8.1':'enumSV_Windows_8_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 = 'http://www.catalog.update.microsoft.com/Search.aspx?q=' + kb_number
    content = requests.get(url).content
    data = re.findall('goToDetails\("(.*)"\)', content)
    data4 = []
    for i in data:
        url1 = 'http://www.catalog.update.microsoft.com/ScopedViewInline.aspx?updateid=' + 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中,
        #最后将VulList添加到根节点VulExpiration中
        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)
数据貌似是错的,但是代码有可借鉴的地方。
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐