Uzyskaj tekst u rodzica bez dzieci za pomocą cheerio
Próbuję wyodrębnić tylko zawartość div - bez żadnego z dzieci tego div - używając cheerio. Jeśli użyję div.text () - dostanę cały tekst - rodzic i dzieci. Oto HTML - chcę tylko wartość „5.25”
Poniższy kod zwraca obecnie „cenę zakupu 5,25 USD”
Poniższy kod HTML:
<div class="outer tile">
< ... various other html here >
<div class="cost">
<span class="text">Purchase price </span>
<small><div class="outer tile">
< ... various other html here >
<div class="cost">
<span class="text">Purchase price </span>
<small>$</small>5.25
</div>
</div>
lt;/small>5.25
</div>
</div>
z wyciągiem odpowiedniego kodu CHEERIO node.js poniżej:
var $ = cheerio.load(data);
$("div.outer.tile").each(function(i, e) {
var price = $(e).find('div.price');
console.log(price.text());
});