gpt4 book ai didi

python - 将 xpath 与 lxml.etree._ElementTree 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:02 24 4
gpt4 key购买 nike

我有一个对象 arptree,我试图找到一个 ip 地址的 mac 地址,但失败了。

>>> arptree 
<lxml.etree._ElementTree object at 0x0000000004641688>

当我尝试以下 xpath 时,它返回一个空列表

>>> arptree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")
[]

如果我修改 xpath 以排除“='10.69.119.150'”,它实际上会返回一个元素列表。

>>> arptree.xpath("descendant::mac-address[following-sibling::ip-address]")                      

[,,,,,,,,,,,]

我可以使用 for 循环来访问内容。我确定 ip 地址 10.69.119.150 的 mac 地址在那里。

for elt in arptree.iter():
print elt.tag, elt.text

奇怪的是,如果我将 xml 输出复制并粘贴到 xml 文件中。然后使用:

from lxml import etree
tree = etree.parse(open('arp.xml'))
tree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")

它将返回 ip 地址的 mac 地址。

我正在使用带有 lxml 包的 Python 2.7.9。谁能帮忙?

更新 1:示例 XML

<arp-table-information>
<arp-table-entry>
<mac-address>00:a0:a5:76:1a:96</mac-address>
<ip-address>10.69.119.130</ip-address>
<hostname>10.69.119.130</hostname>
<interface-name>vlan.49</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
<mac-address>00:0f:bb:c6:26:3d</mac-address>
<ip-address>10.69.119.150</ip-address>
<hostname>10.69.119.150</hostname>
<interface-name>vlan.55</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
</arp-table-information>

更新 2:请忽略更新 1

当我使用

arptree = ex.device.rpc.get_arp_table_information().getroottree()
arptree.write('arptree.xml', pretty_print=True)

要将 ElementTree 保存到 xml,布局更改为

<arp-table-information style="normal">
<arp-table-entry>
<mac-address>
00:a0:a5:76:1a:96
</mac-address>
<ip-address>
10.69.119.130
</ip-address>
<hostname>
10.69.119.130
</hostname>
<interface-name>
vlan.49
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
<mac-address>
00:0f:bb:c6:26:3d
</mac-address>
<ip-address>
10.69.119.150
</ip-address>
<hostname>
10.69.119.150
</hostname>
<interface-name>
vlan.55
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>

也许这就是以下代码不起作用的原因???

arptree.xpath("descendant::mac-address[following-sibling::ip-address='10.69.119.150']")

基于这个 xml 文件,任何人都可以帮忙吗?

最佳答案

在第二个 XML 中,IP 地址值前后有换行符。您可以使用 normalize-space() 函数来修复它:

descendant::mac-address[following-sibling::ip-address[normalize-space()='10.69.119.150']]

演示的工作示例:

from lxml import etree

xml = """<arp-table-information style="normal">
<arp-table-entry>
<mac-address>
00:a0:a5:76:1a:96
</mac-address>
<ip-address>
10.69.119.130
</ip-address>
<hostname>
10.69.119.130
</hostname>
<interface-name>
vlan.49
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
<arp-table-entry>
<mac-address>
00:0f:bb:c6:26:3d
</mac-address>
<ip-address>
10.69.119.150
</ip-address>
<hostname>
10.69.119.150
</hostname>
<interface-name>
vlan.55
</interface-name>
<arp-table-entry-flags>
<none/>
</arp-table-entry-flags>
</arp-table-entry>
</arp-table-information>"""

root = etree.fromstring(xml)
result = root.xpath("descendant::mac-address[following-sibling::ip-address[normalize-space()='10.69.119.150']]")
for r in result:
print(etree.tostring(r))

输出:

<mac-address>
00:0f:bb:c6:26:3d
</mac-address>

关于python - 将 xpath 与 lxml.etree._ElementTree 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com