LXML:KeyError:'@'-element.find('./@ attrname')

问题描述 投票:0回答:1

我不知道如何,但element.find('./@attrname')筹集了KeyError: '@'

code

    import lxml.etree as ET
    with open(self.filepaths[0]) as f:
        root = ET.parse(f)
        root = root.getroot()

        namespaces = {'xmlns': 'http://www.iai-shop.com/developers/iof/extensions.phtml',
                      "xml": "http://www.w3.org/XML/1998/namespace"}
        items = root.findall('.//product', namespaces)

        for item in items:
            name = item.find('./description/short_desc[@xml:lang="pol"]', namespaces).text

            try:
                desc = item.find('./description/long_desc[@lang="pol"]', namespaces).text
            except AttributeError:
                desc = None

            code = item.find('./@code_on_card',namespaces)

我正在尝试从code_on_card获取<product type="xxx" id="xxx" vat="23.0" code_on_card="xxx">属性>

您知道问题出在哪里吗?

[我不知道,但是element.find('./@ attrname')引发KeyError:'@'代码将lxml.etree导入为ET,而open(self.filepaths [0])设为f:root = ET。 parse(f)root = root.getroot()...

python xml xpath lxml
1个回答
0
投票

答案很简单-我使用.find而不是.xpath

© www.soinside.com 2019 - 2024. All rights reserved.