I am trying to get a value from inside page.evaluate()
body in my YouTube scraper that I've built using Puppeteer. I am unable to return the result from page.evaluate()
. How do I achieve this? Here's the code:
let boxes2 = []
const getData = async() => {
return await page.evaluate(async () => { // scroll till there's no more room to scroll or you get at least 250 boxes
console.log(await new Promise(resolve => {
var scrolledHeight = 0
var distance = 100
var timer = setInterval(() => {
boxes = document.querySelectorAll("div.style-scope.ytd-item-section-renderer#contents > ytd-video-renderer > div.style-scope.ytd-video-renderer#dismissable")
console.log(`${boxes.length} boxes`)
var scrollHeight = document.documentElement.scrollHeight
window.scrollBy(0, distance)
scrolledHeight += distance
if(scrolledHeight >= scrollHeight || boxes.length >= 50){
clearInterval(timer)
resolve(Array.from(boxes))
}
}, 500)
}))
})
}
boxes2 = await getData()
console.log(boxes2)
The console.log
wrapping the promise prints the resulting array in the browser's console. I just cannot get that array in boxes2
down where I'm calling the getData()
function.
I feel like I'm missing out on a tiny little bit, but can't figure out what it is. Appreciate any tip here.