用ElementT解析Python XML

2024-09-30 18:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我查看了大量关于我遇到的问题的帖子,并尝试了各种方法来获得我所需要的值,尽管我已经从XML文档中识别了标记,但我似乎不知道如何打印<cve>VALUE</cve>中保存的值。我看到的所有帖子都与属性值相关,因为我的标签没有属性值,我不知道如何获得我想要的值。在

我正在用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>

当前代码

^{pr2}$

代码的示例输出

<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>

Tags: thetoname示例datedomainwindowsxml
1条回答
网友
1楼 · 发布于 2024-09-30 18:21:00

我已经解决了。我只需要文本值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')

相关问题 更多 >