python - 在python中解析xml文件的数据

标签 python xml xml-parsing elementtree

我有以下 xml 文件:

<address addr="x.x.x.x" addrtype="ipv4"/>
<hostnames>
</hostnames>
<ports><port protocol="tcp" portid="1"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port>
<port protocol="tcp" portid="64623"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port>
</ports>
<times srtt="621179" rttvar="35357" to="762607"/>
</host>
<host starttime="1418707433" endtime="1418707742"><status state="up" reason="syn-ack" reason_ttl="0"/>
<address addr="y.y.y.y" addrtype="ipv4"/>
<hostnames>
</hostnames>
<ports><port protocol="tcp" portid="1"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port>
<port protocol="tcp" portid="64680"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port>
</ports>
<times srtt="834906" rttvar="92971" to="1206790"/>
</host>
<host starttime="1418707433" endtime="1418707699"><status state="up" reason="syn-ack" reason_ttl="0"/>
<address addr="w.w.w.w" addrtype="ipv4"/>
<hostnames>
</hostnames>
<ports><extraports state="filtered" count="997">
<extrareasons reason="no-responses" count="997"/>
</extraports>
<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="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port>
<port protocol="tcp" portid="7443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="oracleas-https" method="table" conf="3"/></port>
</ports>
<times srtt="690288" rttvar="110249" to="1131284"/>
</host>

我尝试为每个 ip 提取数据的是:

import sys
import xml.etree.ElementTree as ET
input=sys.argv[1]

tree=ET.parse(input)
root=tree.getroot()

for host in root.findall('host'):
    updown=host.find('status').get('state')
    if updown=='up':
        print 'IP Address: '+host.find('address').get('addr')
        ports=[port.get('portid') for port in root.findall('.//port')]
        state=[port.get('state') for port in root.findall('.//port/state')]
        name=[port.get('name') for port in root.findall('.//port/service')]

但是它返回了我的所有ips信息。如何获取每个IP的具体信息?

我认为我应该更改 root.findall 但我不知道如何做到这一点。

最佳答案

在循环中只需将 root.findall() 更改为 host.findall():

for host in root.findall('host'):
    updown=host.find('status').get('state')
    if updown=='up':
        print 'IP Address: '+host.find('address').get('addr')
        ports=[port.get('portid') for port in host.findall('.//port')]
        state=[port.get('state') for port in host.findall('.//port/state')]
        name=[port.get('name') for port in host.findall('.//port/service')]

这将限制查找每个主机内的端口、状态和名称,而不是整个 XML 文档中的端口、状态和名称。

关于python - 在python中解析xml文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27502940/

相关文章:

python - 为django中的所有模板加载静态文件

xml - 无法使用 logstash 过滤器解析 xml 输入

c++ - 如何使用 DOM xerces c++.. 在 xml 文件中查找元素名称?

python - 将 for 循环放入函数中时出现问题 (Python3)

python - AppEngine 的功能和技巧

php - 我有一个 iframe,我需要 Google 对其内容进行索引。这可能吗?

java - 为什么 STAX 解析器认为这是有效的 XML 1.0 而不是 1.1?

python - 在 Python 中处理格式错误的 XML

python - Python 中的 MD5 哈希

c# - 循环遍历 XDocument 的元素并获取特定属性