Search code examples
javascriptweb-scrapingcheerio

How to scrape a particular element (Cheerio)


The website code is <div class="product__price product__price-promo "><del>16 999</del>15 999</div>

I need to scrape 15 999. How can I do this exactly?

I already tried to do it with selector div.product__price or product__price product__price-promo in which case I get the result "1699915999"

If I try with selector div.product__price > del get the result 16999.

So far I don't know how to get 15 999


Solution

  • You can do:

    const b = priceSelectors.map(sel => [...this._$(sel)[0].childNodes].find(n=>n.nodeType==3&&n.data.trim()).data.trim()).find(e=>e) || null;
    

    This goes through the selectors and finds the content of the first non-whitespace text node.