How to get first to fifth element's tag data with CheerIo

Viewed 270

I scrape HTML data with CheerIO in Node.js.

And I got a HTML data like this. And I want to get all image src's data

<div class="owl-stage-outer">
    <div class="owl-stage">
        <div class="owl-item active">
            <div class="tb-thumb-item tb-selected">
                <a>
                    <img src="IMAGE URL">
                </a>
            </div>
        </div>
        <div class="owl-item active">
            <div class="tb-thumb-item">
                <a>
                    <img src="IMAGE URL">
                </a>
            </div>
        </div>
        <div class="owl-item active">
            <div class="tb-thumb-item ">
                <a>
                    <img src="IMAGE URL">
                </a>
            </div>
        </div>
        .........(Same owl-item classes)
    </div>
</div>

So I copy JS path from Chrome Developer Tool And I thought with this code it might work. But it only returns 'undefined'

await got(`${URL}`, {
    timeout: {
      request: 30000
    }
  }).then((response) => {
    const $ = cheerio.load(response.body);

console.log($('#thumblist > div.owl-stage-outer > div.olw-stage > div.owl-item:nth-child(1) > div.tb-thumb-item > a > img').attr('src'));

console.log($('#thumblist > div.owl-stage-outer > div.olw-stage > div.owl-item:nth-child(2) > div.tb-thumb-item > a > img').attr('src'));

console.log($('#thumblist > div.owl-stage-outer > div.olw-stage > div.owl-item:nth-child(3) > div.tb-thumb-item > a > img').attr('src'));

});

But it's really weird that this code works, but I need to get all of them but, this code only gets first element data.

console.log($('.tb-thumb .tb-thumb-item a > img:nth-child(1)').attr('src');
0 Answers
Related