我如何从 puppeteer 中的 page.evaluate() 返回一个值? [英] How do i return a value from page.evaluate() in puppeteer?
问题描述
我正在尝试从使用 Puppeteer 构建的 YouTube 抓取工具中的 page.evaluate()
正文中获取值.我无法从 page.evaluate()
返回结果.我如何实现这一目标?代码如下:
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)
包装承诺的 console.log
在浏览器的控制台中打印结果数组.我只是无法在 boxes2
中调用 getData()
函数的地方获取该数组.我觉得我错过了一点点,但无法弄清楚它是什么.感谢这里的任何提示.
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.
推荐答案
小问题是你实际上并没有从 page.evaluate 内部返回数据:
The little issue is that you don't actually return the data from inside of page.evaluate:
const getData = () => {
return page.evaluate(async () => {
return await new Promise(resolve => { // <-- return the data to node.js from browser
// scraping
}))
})
}
这里是 puppeteer 的一个完整的最小工作示例,它将打印数组 [ 1, 2, 3 ]
:
And here's a full minimal working example for puppeteer that will print array [ 1, 2, 3 ]
:
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
boxes2 = [];
const getData = async() => {
return await page.evaluate(async () => {
return await new Promise(resolve => {
setTimeout(() => {
resolve([1,2,3]);
}, 3000)
})
})
}
boxes2 = await getData();
console.log(boxes2)
await browser.close();
});
这篇关于我如何从 puppeteer 中的 page.evaluate() 返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!