简体   繁体   中英

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?

Using querySelectorAll:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM