lxml :while循环与节点相同的元素

lxml : while loop with node same elements

我正在使用 lxml 基于 xml 创建一个 json 文件。 xml 文件具有这种结构:

<spots_list>
    <spot id="001" latitude="2011464" longitude="979511">
        <adress>Somewhere</adress>
        <city>BOSTON</city>
        <price category="Intermediate" value="782"/>
        <price category="Expensive" value="2765"/>
        <price category="Cheap" value="12"/>
     </spot>
    <spot id="002" latitude="2101644" longitude="915971">
        <adress>Somewhere else (very very far away)</adress>
        <city>CAMBRIDGE</city>
        <price category="Intermediate" value="472"/>
        <price category="Intermediate (but less expensive)" value="422"/>
        <price category="Expensive" value="20275"/>
        <price category="Cheap" value="12"/>
     </spot>
</spots_list>

every 中价格元素的数量可以改变,所以我尝试在 Python 中使用 while 循环。这是关联代码:

from lxml import etree

tree = etree.parse("my_file.xml")

for node in tree.xpath("//spots_list/spot"):
    for adress in node.xpath("adress"):
        adr = adress.text
    while node.xpath("price"):
        print(adr)

我知道这是错误的,因为第一个地址一遍又一遍地出现,但我不知道如何制定这个循环来切换下一个元素...

提前致谢。

while 语句中的基本问题是 node.xpath(...) returns 一个列表,如果不为空则认为是 True。您只需要做与顶层相同的事情,即遍历您感兴趣的元素,例如

def parse_spot(el):
    adr = el.find('adress')
    return dict(
        address=adr.text if adr is not None else None,  # error handling if not found
        price=[dict(p.attrib) for p in el.findall('price')]
    )

tree = etree.fromstring(xml)  # xml is your example as string

[parse_spot(el) for el in tree.findall('./spot')]

您也可以像以前那样使用 xpath 而不是 findall