I am doing some web scraping in JS for the first time and already stuck.
I have a cheerio object that contains div tags I want to pull out. If I iterate through the cheerio using the each method, I can see they are all there
temp.each(function (i, e) {
console.log($(e).text());
console.log("...looping");
console.log(i);
});
...looping
0
“The square root of the amount of ducks I give"
...looping
1
Sweet Home, Alabama
...looping
2
FOOTER GOES HERE
...looping
3
I see that quote I want is under the element indexed at 1, but if I try to grab it with temp.get(1).text(), I get TypeError: temp.get(...).text is not a function.
There is something big I am missing here.
.get gives you the element, not the Cheerio object - elements don't have a .text method. Either use the proper property to extract text from an element:
console.log($(e).get(1).textContent)
Or use .eq instead, which will give you a Cheerio object containing only the element at that index.
console.log($(e).eq(1).text())