2

我想使用 Xidel 从此代码中提取数字/浮点值:

<p class="price">
    <span class="woocommerce-Price-amount amount">
        <bdi>
            304.00
            <span class="woocommerce-Price-currencySymbol">
                €
            </span>
        </bdi>
    </span>
</p>

我正在尝试以下命令:

xidel -s '<p class="price"><span class="woocommerce-Price-amount amount"><bdi>304.00 <span class="woocommerce-Price-currencySymbol">€&lt;/span></bdi></span></p>' -e "//p[@class='price']/translate(normalize-space(substring-before(., '€')),' ','')"

translate 命令应该替换空格,但它不起作用,在输出中我仍然看到数字“304.00_”后面有一个空格。

4

2 回答 2

1

尝试将 xpath 表达式更改为

-e  "substring-before(//p[@class='price']//bdi/normalize-space(.),' ')"

或者

 -e "substring-before(//p[@class='price']//bdi/.,' ')"

或使用tokenize()

 -e "tokenize(//p[@class='price']//bdi/.,' ')[1]"

输出应该是

'304.00'
于 2021-12-13T00:05:45.740 回答
1

您将不得不使用以下查询之一单独处理不间断 空间:

-e "//p[@class='price']/span/bdi/substring-before(text(),'&#160;')"
-e "//p[@class='price']/span/bdi/translate(text(),x:cps(160),'')"
-e "//p[@class='price']/span/bdi/replace(text(),'&#xA0;','')"

不能用normalize-space(),因为...

https://www.w3.org/TR/xpath-functions-31/#func-normalize-space

[Extensible Markup Language (XML) 1.1 Recommendation]中空白的定义没有改变。为方便起见,在此重复:

S ::= (#x20 | #x9 | #xD | #xA)+

...它处理空格、制表符、回车和换行,但不处理不间断空格:

xidel -s "<x>   test   </x>" -e "x'[{x}]'"
[   test   ]

xidel -s "<x>   test   </x>" -e "x'[{normalize-space(x)}]'"
[test]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{x}]'"
[   test   ]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{normalize-space(x)}]'"
[   test   ]

xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{translate(x,'&#160;','')}]'"
xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{replace(x,x:cps(160),'')}]'"
xidel -s "<x>&nbsp;&nbsp;&nbsp;test&nbsp;&nbsp;&nbsp;</x>" -e "x'[{replace(x,'&#xA0;','')}]'"
[test]

顺便说一句,在该网站上获取价格的替代方法:

xidel -s "https://kenzel.sk/produkt/bicykle/zivotny-styl/signora/" -e ^"^
  parse-json(^
    //body/script[@type='application/ld+json']^
  )//priceSpecification/price^
"
304.00
于 2021-12-13T23:25:31.570 回答