使用ElementTree进行Python XML解析

时间:2015-04-20 22:58:48

标签: python xml elementtree

首先,我检查了大量关于我遇到的问题的帖子,并尝试了各种方法来获取我需要的值,即使我已经从XML文档中识别出标签我似乎无法解决如何打印价值保持在<cve>VALUE</cve>。我看到的所有帖子都与具有attrib值的项目有关,因为我的标签没有attrib值我不知道如何获得我追求的价值。

我正在使用Elementtree和python解析一个nessus文件。

我可以识别包含该值的标记,但我无法获得该值,非常令人沮丧。似乎没有attrib值(它的空白),但正如您可以通过XML示例看到的那样有一个值(但我假设它不是attrib值)。任何指针都会受到赞赏。

示例XML

<SNIP>
<ReportItem port="445" svc_name="cifs" protocol="tcp" severity="0" pluginID="10398" pluginName="Microsoft Windows SMB LsaQueryInformationPolicy Function NULL Session Domain SID Enumeration" pluginFamily="Windows">
<bid>959</bid>
<cve>CVE-2000-1200</cve>
<description>By emulating the call to LsaQueryInformationPolicy() it was possible to obtain the domain SID (Security Identifier).
The domain SID can then be used to get the list of users of the domain</description>
<fname>smb_dom2sid.nasl</fname>
<osvdb>715</osvdb>
<plugin_modification_date>2015/01/12</plugin_modification_date>
<plugin_name>Microsoft Windows SMB LsaQueryInformationPolicy Function NULL Session Domain SID Enumeration</plugin_name>
<plugin_publication_date>2000/05/09</plugin_publication_date>
<plugin_type>local</plugin_type>
<risk_factor>None</risk_factor>
<script_version>$Revision: 1.51 $</script_version>
<solution>n/a</solution>
<synopsis>It is possible to obtain the domain SID.</synopsis>
<xref>OSVDB:715</xref>
<plugin_output>The remote domain SID value is :
FAKESTUFF HERE</plugin_output>
</ReportItem>
<SNIP>

当前代码

import elementtree.ElementTree as ET

def getCVE(nessus_file):
try:
    tree = ET.parse(nessus_file)
    doc = tree.getroot()
    walk = doc.getiterator('cve')
    for cve in walk:
        print cve
except:
    pass


getCVE('file.nessus')

代码输出示例

<Element cve at 10fd05170>
<Element cve at 10fd20f38>
<Element cve at 10fd2c200>
<Element cve at 10fd3ea70>
<Element cve at 10fd44a70>
<Element cve at 10fd44b00>
<Element cve at 10fd5c170>
<Element cve at 10fd767e8>
<Element cve at 10fdbf290>
<Element cve at 10fdce440>
<Element cve at 10fdce4d0>
<SNIP>

1 个答案:

答案 0 :(得分:1)

我已经解决了^ _ ^。我只需要文本值lol YAY!耗费数小时才能解决这个问题

所以新的工作代码是

import elementtree.ElementTree as ET

def getCVE(nessus_file):
try:
    tree = ET.parse(nessus_file)
    doc = tree.getroot()
    walk = doc.getiterator('cve')
    for cve in walk:
        print cve.text
except:
    pass

getCVE('file.nessus')