Search code examples
javascriptweb-scrapingcheerio

Can I select again from element variable in Cheerio?


I just started using Cheerio. How can I select again from a loop element?

const championBox = $('div.css-1s4j24f>div');
championBox.each(async function(index, element) {
    console.log(
    // I want to get element>div.something>span.somethingelse
    )
})

Thanks in advance.


Solution

  • You can use .find, it is the cheerio equivalent of .querySelectorAll.
    Using > at the start of the string makes it behave like element > div.something ...

    const championBox = $('div.css-1s4j24f>div');
    championBox.each(async function(index, element) {
        console.log(
            $(element).find('>div.something>span.somethingelse')
        )
    })