在python中从xml格式中提取数据

时间:2014-12-15 15:46:29

标签: python xml xml-parsing elementtree nmap

我将以下nmap输出作为xml格式:

<ports><extraports state="closed" count="991">
<extrareasons reason="conn-refused" count="991"/>
</extraports>
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
<port protocol="tcp" portid="139"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="netbios-ssn" method="table" conf="3"/></port>
<port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port>

我想获得打开的端口号:

print 'Port Number: '+host.find('ports').find('port').get('portid')

但结果只是22

我如何获得结果:

22
25
139
443

1 个答案:

答案 0 :(得分:1)

查找所有port个元素,并获取portid个属性。

使用Element.findalllist comprehension

>>> import xml.etree.ElementTree as ET
>>> root = ET.fromstring('''
<ports><extraports state="closed" count="991">
<extrareasons reason="conn-refused" count="991"/>
</extraports>
<port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="ssh" method="table" conf="3"/></port>
<port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port>
<port protocol="tcp" portid="139"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="netbios-ssn" method="table" conf="3"/></port>
<port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port>
</ports>
''')
>>> [port.get('portid') for port in root.findall('.//port')]
['22', '25', '139', '443']