Neik0
Neik0

Reputation: 103

Cheerio How do I iterate through the queryselector?

I am trying to iterate through td[class="titleColumn"]. Here is my current code.

const puppeteer = require('puppeteer');

(async () => {

    let movieURL = 'https://www.imdb.com/chart/top/?ref_=nv_mv_250';

    let browser = await puppeteer.launch({ headless: false});
    let page = await browser.newPage();

    await page.goto(movieURL, {waitUntil: 'networkidle2'});

    let data = await page.evaluate(() => {
        let title = document.querySelector('td[class="titleColumn"]').innerText;

        return{
            title
        }
    })

    console.log(data);

    await browser.close()
})();

I only get one title. How would I iterate through it?

Upvotes: 0

Views: 434

Answers (2)

pguardiario
pguardiario

Reputation: 54992

Using querySelectorAll:

[...document.querySelectorAll('td[class="titleColumn"]')].map(td => td.innerText)

Upvotes: 2

Lyokolux
Lyokolux

Reputation: 1417

This is normal because document.querySelector stops at the first item found. Use document.querySelectorAll instead and it should work.

Upvotes: 0

Related Questions