Javascript 用cheerio从html元素中提取两个文本值
使用cheerio,$被定义为cheerio对象,我试图从一些元素中获取两个文本(当前价格和原始价格),这些元素在html中只定义了类而没有id。你知道如何做到这一点吗 下面是包含这两个值的html内容片段Javascript 用cheerio从html元素中提取两个文本值,javascript,jquery,cheerio,Javascript,Jquery,Cheerio,使用cheerio,$被定义为cheerio对象,我试图从一些元素中获取两个文本(当前价格和原始价格),这些元素在html中只定义了类而没有id。你知道如何做到这一点吗 下面是包含这两个值的html内容片段 <div class="buy-box__element"> <div class="clp-component-render"> <div class="clp-component-rende
<div class="buy-box__element">
<div class="clp-component-render">
<div class="clp-component-render">
<div class="ud-component--course-landing-page-udlite--price-text" ng-non-bindable="">
<div>
<div class="price-text--container--Ws-fP udlite-clp-price-text" data-purpose="price-text-container">
<div class="price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xl" data-purpose="course-price-text"><span class="udlite-sr-only">Current price</span><span><span>₹700</span></span></div>
<div class="price-text--price-part--Tu6MH price-text--original-price--2e-F5 udlite-clp-list-price udlite-text-sm" data-purpose="original-price-container">
<div data-purpose="course-old-price-text"><span class="udlite-sr-only">Original Price</span><span><s><span>₹1,280</span></s></span></div>
</div>
<div class="price-text--price-part--Tu6MH udlite-clp-percent-discount udlite-text-sm" data-purpose="discount-percentage"><span class="udlite-sr-only">Discount</span><span>45% off</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
你能试试这个吗
html
应该是内部html,您可以使用类似于木偶的库代码>
对于原价,将当前价格替换为原价您可以执行以下操作:
$('span:contains("Current price") + span span').text()
$('span:contains("Current price")', html).each(function() {
let CurrentPrice1 = $(this).next().text();
let CurrentPrice2 = Number(CurrentPrice1.replace(/[^0-9.-]+/g,""));
console.log(CurrentPrice1); //this with symbol
console.log(CurrentPrice2); //this for only fetching the numeric value
});
$('span:contains("Current price") + span span').text()