【发布时间】:2022-01-20 17:06:40
【问题描述】:
我正在尝试使用 Python lxml 解析器从 ONIX XML format 文件中提取一些信息。
除此之外,我对文档感兴趣的部分如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ProductSupply>
<SupplyDetail>
<Supplier>
<SupplierRole>03</SupplierRole>
<SupplierName>EGEN</SupplierName>
</Supplier>
<ProductAvailability>40</ProductAvailability>
<Price>
<PriceType>01</PriceType>
<PriceAmount>0.00</PriceAmount>
<Tax>
<TaxType>01</TaxType>
<TaxRateCode>Z</TaxRateCode>
<TaxRatePercent>0</TaxRatePercent>
<TaxableAmount>0.00</TaxableAmount>
<TaxAmount>0.00</TaxAmount>
</Tax>
<CurrencyCode>NOK</CurrencyCode>
</Price>
<Price>
<PriceType>02</PriceType>
<PriceQualifier>05</PriceQualifier>
<PriceAmount>0.00</PriceAmount>
<Tax>
<TaxType>01</TaxType>
<TaxRateCode>Z</TaxRateCode>
<TaxRatePercent>0</TaxRatePercent>
<TaxableAmount>0.00</TaxableAmount>
<TaxAmount>0.00</TaxAmount>
</Tax>
<CurrencyCode>NOK</CurrencyCode>
</Price>
</SupplyDetail>
</ProductSupply>
我需要在以下条件下领取价格金额:
PriceType='02' and CurrencyCode='NOK' and PriceQualifier='05'
我试过了:
price = p.find(
"ProductSupply/SupplyDetail[Supplier/SupplierRole='03']/Price[PriceType='02' \
and CurrencyCode='NOK' and PriceQualifier='05']/PriceAmount").text
由于某种原因,我的带有 and 运算符的 XPath 无法正常工作并出现以下错误:
File "<string>", line unknown
SyntaxError: invalid predicate
知道如何处理它吗? 非常感谢任何帮助!
【问题讨论】:
-
尝试使用
.xpath()而不是.find()。