【问题标题】:Xidel extract number/floatXidel 提取数/浮点数
【发布时间】:2022-01-16 13:38:24
【问题描述】:

我想使用 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">€</span></bdi></span></p>' -e "//p[@class='price']/translate(normalize-space(substring-before(., '€')),' ','')"

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

【问题讨论】:

    标签: xpath xidel


    【解决方案1】:

    您将不得不使用以下查询之一单独处理 no-break space

    -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
    

    【讨论】:

      【解决方案2】:

      尝试将 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'
      

      【讨论】:

      • 它适用于上面的示例,但由于某种原因,实际的 website(使用相同的代码)无法正常工作。很奇怪。
      • @Adrian Weird 确​​实。无论实际网站上的内容是什么,它都不是任何常规空格字符。也许更聪明的人可以解决这个问题。同时,有一种方法可以检索该数据,但它非常笨拙。如果您有兴趣,我可以编辑答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多