首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xpath使用python过滤XML文件

使用xpath使用python过滤XML文件
EN

Stack Overflow用户
提问于 2017-07-12 18:59:34
回答 3查看 921关注 0票数 0

我正在尝试从一个XML文件中获取一些数据,该文件是我在测试环境中执行网络扫描后生成的。我得到的XML文件为我提供了大量要过滤的信息。

我唯一感兴趣的系统是状态为"up“的系统

代码语言:javascript
复制
from xml.etree.ElementTree import ElementTree
mydoc = ElementTree(file='nmap_output.xml')
for e in mydoc.findall("./nmaprun/host/state[@state='up']"):
print (e.get('title').text )

下面我发布了我正在使用的XML文件的一部分,它太大了,无法完整地发布它。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nmaprun>
<?xml-stylesheet href="file:///C:/Program Files (x86)/Nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 7.40 scan initiated Mon Jul 10 18:24:16 2017 as: nmap -sS -O -vv -oX c:\\drv\\1921689024.xml 192.168.9.0/24 -->
<nmaprun scanner="nmap" args="nmap -sS -O -vv -oX c:\\drv\\1921689024.xml 192.168.9.0/24" start="1499703856" startstr="Mon Jul 10 18:24:16 2017" version="7.40" xmloutputversion="1.04">
<taskbegin task="Ping Scan" time="1499703857"/>
<taskend task="Ping Scan" time="1499703860" extrainfo="256 total hosts"/>
<taskbegin task="Parallel DNS resolution of 256 hosts." time="1499703860"/>
<taskend task="Parallel DNS resolution of 256 hosts." time="1499703860"/>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.0" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.2" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.3" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.4" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.5" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.6" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.9" addrtype="ipv4"/>
</host>
<host><status state="down" reason="no-response" reason_ttl="0"/>
<address addr="192.168.9.10" addrtype="ipv4"/>
</host>
<host starttime="1499703857" endtime="1499704025"><status state="up" reason="echo-reply" reason_ttl="249"/>
<address addr="192.168.9.1" addrtype="ipv4"/>
<hostnames>
<hostname name="man-nas-01.man.eu.cascorp.biz" type="PTR"/>
</hostnames>
<ports><extraports state="closed" count="991">
<extrareasons reason="resets" count="991"/>
</extraports>
</host>
<host starttime="1499703857" endtime="1499704025"><status state="up" 
reason="echo-reply" reason_ttl="249"/>
<address addr="192.168.9.7" addrtype="ipv4"/>
<hostnames>
</hostnames>
</nmaprun>

有没有人能告诉我怎样才能只让主机处于"up“状态?

EN

回答 3

Stack Overflow用户

发布于 2017-07-12 19:13:24

这将查找具有<status state="up">子级的所有<host>标记。

代码语言:javascript
复制
import xml.etree.ElementTree as ET
tree = ET.parse('nmap_output.xml')
root = tree.getroot()
print(root.findall(".//status[@state='up']/.."))

(在Python docs上找到示例)

票数 0
EN

Stack Overflow用户

发布于 2017-07-12 19:13:45

看起来您应该能够使用以下xpath获取主机

代码语言:javascript
复制
mydoc.findall('.//host/status[@state="up"]/..')

如果你想获得你可以使用的地址

代码语言:javascript
复制
addresses = mydoc.findall('.//host/status[@state="up"]/../address')

for address in addresses:
    print(address.get('addr'))
票数 0
EN

Stack Overflow用户

发布于 2017-07-14 22:12:39

Question:...如何才能只获取状态为"up“的主机?

注意:您的示例XML是无效的,原因如下:附加的<!DOCTYPE nmaprun>只能是one

缺少结束</ports>

缺少结束</host>

代码语言:javascript
复制
from xml.etree.ElementTree import ElementTree

mydoc = ElementTree(file='nmap_output.xml')

# Find all <host> Elements with <status state="up" ...>
for host in mydoc.findall('.//host/status[@state="up"]/..'):

    # Get the Attributes Dict from <host> <status ...> and <address ...>
    status = host.find('.//status').attrib
    address = host.find('.//address').attrib

    print ('host: addr={a[addr]}, status: {s[state]} reason={s[reason]}'.
             format(a=address, s=status))

Qutput主机: addr=192.168.9.1,状态:启动原因=回应回复主机: addr=192.168.9.7,状态:启动原因=回应回复

使用Python测试的:3.4.2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45055796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档