Puppeteer:脚本似乎闲置了
Puppeteer : script seems to go iddle
我目前正在编写一个脚本,用于截取页面内每个 dividual div 的屏幕截图。
该脚本有效,但每次迭代后速度变慢。例如,第 11 次迭代需要几分钟,而第一次只需要几秒钟。此外,脚本实际上从未因错误而停止,我相信如果我不中断它,它将永远 运行。
你们中有人遇到过同样的问题吗?有没有办法解决这个问题?我是 promises 和 Puppeteer 的新手...
提前致谢!
下面是我的函数代码和执行输出。
函数代码如下:
async function takeScreenshot(bookPage){
await bookPage.evaluate(() => {
document.body.style.position = "relative";
document.querySelector("#page-container").style.position = "relative";
});
for(var i = 0; i < allPagesDimensions.length; i++){
console.log("Itération n°" + i);
await bookPage.evaluate((i) => {
const page = document.querySelector("#pf" + (i + 1));
window.scrollTo(0, page.offsetTop);
}, i);
const pageDimensions = await bookPage.evaluate((i) => {
const page = document.querySelector("#pf" + (i + 1));
var returnValue = { x: page.offsetLeft, y: page.offsetTop, width: page.clientWidth, height: page.clientHeight };
return returnValue;
}, i);
console.log("Lancement de la capture");
await bookPage.screenshot({
path: "page-" + i + ".png",
clip: { "x": pageDimensions.x, "y": pageDimensions.y, "width": pageDimensions.width, "height": pageDimensions.height }
});
console.log("Capture réussie");
}
}
输出如下:
Itération n°0
Lancement de la capture
Capture réussie
Itération n°1
Lancement de la capture
Capture réussie
Itération n°2
Lancement de la capture
Capture réussie
Itération n°3
Lancement de la capture
Capture réussie
Itération n°4
Lancement de la capture
Capture réussie
Itération n°5
Lancement de la capture
Capture réussie
Itération n°6
Lancement de la capture
Capture réussie
Itération n°7
Lancement de la capture
Capture réussie
Itération n°8
Lancement de la capture
Capture réussie
Itération n°9
Lancement de la capture
Capture réussie
Itération n°10
Lancement de la capture
Capture réussie
Itération n°11
我终于成功实现了这个机器人。
诀窍是将对异步函数的调用包装在 try-catch 中,并在方法抛出异常时返回循环顶部。这样,机器人会一次又一次地尝试,直到 puppeteer 最终设法按照你的要求去做。
使用大页面。
我目前正在编写一个脚本,用于截取页面内每个 dividual div 的屏幕截图。 该脚本有效,但每次迭代后速度变慢。例如,第 11 次迭代需要几分钟,而第一次只需要几秒钟。此外,脚本实际上从未因错误而停止,我相信如果我不中断它,它将永远 运行。
你们中有人遇到过同样的问题吗?有没有办法解决这个问题?我是 promises 和 Puppeteer 的新手...
提前致谢!
下面是我的函数代码和执行输出。
函数代码如下:
async function takeScreenshot(bookPage){
await bookPage.evaluate(() => {
document.body.style.position = "relative";
document.querySelector("#page-container").style.position = "relative";
});
for(var i = 0; i < allPagesDimensions.length; i++){
console.log("Itération n°" + i);
await bookPage.evaluate((i) => {
const page = document.querySelector("#pf" + (i + 1));
window.scrollTo(0, page.offsetTop);
}, i);
const pageDimensions = await bookPage.evaluate((i) => {
const page = document.querySelector("#pf" + (i + 1));
var returnValue = { x: page.offsetLeft, y: page.offsetTop, width: page.clientWidth, height: page.clientHeight };
return returnValue;
}, i);
console.log("Lancement de la capture");
await bookPage.screenshot({
path: "page-" + i + ".png",
clip: { "x": pageDimensions.x, "y": pageDimensions.y, "width": pageDimensions.width, "height": pageDimensions.height }
});
console.log("Capture réussie");
}
}
输出如下:
Itération n°0
Lancement de la capture
Capture réussie
Itération n°1
Lancement de la capture
Capture réussie
Itération n°2
Lancement de la capture
Capture réussie
Itération n°3
Lancement de la capture
Capture réussie
Itération n°4
Lancement de la capture
Capture réussie
Itération n°5
Lancement de la capture
Capture réussie
Itération n°6
Lancement de la capture
Capture réussie
Itération n°7
Lancement de la capture
Capture réussie
Itération n°8
Lancement de la capture
Capture réussie
Itération n°9
Lancement de la capture
Capture réussie
Itération n°10
Lancement de la capture
Capture réussie
Itération n°11
我终于成功实现了这个机器人。
诀窍是将对异步函数的调用包装在 try-catch 中,并在方法抛出异常时返回循环顶部。这样,机器人会一次又一次地尝试,直到 puppeteer 最终设法按照你的要求去做。
使用大页面。