使用 XPATH text() 函数时遇到困难

问题描述 投票:0回答:1

我的目标是从页面获取价格文本。 当我使用 XPATH 时

//*[@id="module_product_price_1"]/div/div/span

,它只找到一项。但是,当我包含 text() 函数时

//*[@id="module_product_price_1"]/div/div/span/text()

,它发现了两项。第一个是隐藏的,而第二个则包含可见文本。

这是网页

这是元素

<div id="module_product_price_1" class="pdp-block module"><div class="pdp-mod-product-price"><img src="https://img.lazcdn.com/g/gcp/lazada/id00780-480-72.jpg_500x500q80.jpg_.webp" alt="promotion" class="pdp-mod-product-price-topbanner"><div class="pdp-product-price"><span class="notranslate pdp-price pdp-price_type_normal pdp-price_color_orange pdp-price_size_xl">$30.00</span></div></div></div>

html xml web-scraping xpath xpath-1.0
1个回答
0
投票

检查

$x('//*[@id="module_product_price_1"]/div/div/span/text()')

显示一个额外的空文本节点作为目标

span
的第一个子节点。

通过获取目标

span
的字符串值并通过
normalize-space()
标准化空格,可以轻松忽略它:

normalize-space(//*[@id="module_product_price_1"]/div/div/span)
© www.soinside.com 2019 - 2024. All rights reserved.