gpt4 book ai didi

Python/Xpath 转换查询

转载 作者:行者123 更新时间:2023-12-01 05:48:44 26 4
gpt4 key购买 nike

我正在尝试 Scrapy。我有以下内容:

hxs.select('//span[contains(@itemprop, "price")]').extract()

输出:

[u'<span itemprop="price" class="offer_price">\n<span class="currency">\u20ac</span>\n16<span class="offer_price_fraction">,95</span>\n</span>']

如何检索此输出:

16.95

换句话说,用分数价格范围添加价格 + 将 , 替换为 。

最佳答案

使用这个单一的 XPath 表达式:

   translate(
concat(//span[@itemprop = 'price']/text()[normalize-space()],
//span[@itemprop = 'price']/span[@class='offer_price_fraction']
),
',',
'.'
)

基于 XSLT 的验证:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:copy-of select=
"translate(
concat(//span[@itemprop = 'price']/text()[normalize-space()],
//span[@itemprop = 'price']/span[@class='offer_price_fraction']
),
',',
'.'
)"/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于此 XML 文档时:

<span itemprop="price" class="offer_price">
<span class="currency">\u20ac</span>
16<span class="offer_price_fraction">,95</span>
</span>

计算 XPath 表达式并将计算结果复制到输出:

16.95

关于Python/Xpath 转换查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177311/

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