gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:41:14 25 4
gpt4 key购买 nike

我不知道怎么办,但是element.find('./@attrname')加薪KeyError: '@'

代码

    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"> 的属性

你知道问题出在哪里吗?

最佳答案

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

编辑:

然后你必须使用xpath获取文本:element.xpath('.//element/text())[0]

关于python - LXML:KeyError: '@' - element.find ('./@attrname'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59448570/

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