1

How can I get the content of an element? I have tried using this

let $ = cheerio.load(response);

let products = $('.product-tile__title');

console.log(products.first().html()); // prints null to console

There is a list of elements having the class name "product-tile__title" I want to get the text in the element. They are in h4 title elements which may make a difference.

Element HTML:

<h4 class="ais-hit--title product-tile__title">HP 15-EQ1048AU 15.6" HD Laptop (256GB)</h4>

Code:

var app = express();

app.get('/test', function(req, res) {
    
    const options = {
        url: `https://www.jbhifi.com.au/collections/computers-tablets/windows-laptops`,
        headers: { 'User-Agent': 'Request-Promise' }
    }

    request(options)
        .then((response, body) => {
            process.stdout.write('testing 123');
            console.log('testing console logger');

            let $ = cheerio.load(response);

            //let products = $('.product-tile__title');

            let arr = $('.product-tile__title').toArray().map((x) => { return $(x).html()});
            for (let i = 0; i < arr.length; i++) {
                console.log(arr[i]);
            }
            console.log(arr.length); // 0

            $('.product-tile__title').each((i,element)=>{
                var omg = $(element).html();
                console.log(omg);
            });

            console.log(response);
            res.write(response);
            res.write('request complete');
            res.end();
        })
        .catch((err) => {
            console.log('error in request');
            console.log(err);
        })

})
ggorlen
  • 44,755
  • 7
  • 76
  • 106
coder
  • 239
  • 2
  • 3
  • 14
  • 3
    Can you provide what you have in `response`? – Khoa Nguyen May 13 '21 at 09:44
  • Response has the whole page html, it is working, response is valid html – coder May 13 '21 at 09:55
  • Nesting of the elements inside an h4 isn't gonna make any difference. Make sure your response is valid and existent. – AdityaParab May 13 '21 at 09:55
  • Does this answer your question? [How can I scrape pages with dynamic content using node.js?](https://stackoverflow.com/questions/28739098/how-can-i-scrape-pages-with-dynamic-content-using-node-js) – ggorlen Jan 04 '23 at 19:49
  • Content appears to be added dynamically via JS, so Cheerio can't help you much here – ggorlen Jan 04 '23 at 19:49
  • `arr.length == 0` should have been the clue -- the selector isn't finding anything. BTW, there's no need for `.toArray()`, jQuery has its own `.map()` method for collections. – Barmar Sep 01 '23 at 22:11

0 Answers0