1
私は人形でデータのページリストのすべての結果を取得したいと思います。Puppeteer page.contentを再帰的反復で取得してページネートリストのすべての結果を取得
私はサイクルのために作る場合、私はこのエラーを与える:
(node:54961) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.evaluate): Cannot find context with specified id undefined
(node:54961) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
これは私のコードです:
const puppeteer = require('puppeteer');
var sleep = require('sleep');
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
console.log('start');
page.on('console', (...args) => console.log('PAGE LOG:', ...args));
await page.goto('pageUrl');
var num = 0;
for(var i=0; i< 10; i++){
var content = await page.content();
console.log('we have content of page '+num);
var fs = require('fs');
fs.writeFileSync("htmls/"+num+".html", content);
console.log("The file of page "+num+" was saved!");
var sleepSecond = getRandomInt(20,40);
console.log("We are waiting "+ sleepSecond + " seconds");
sleep.sleep(sleepSecond);
var inputElement = await page.$('a.next');
await inputElement.click();
console.log('Click on next');
sleepSecond = getRandomInt(40,80);
console.log("We are waiting "+ sleepSecond + " seconds");
sleep.sleep(sleepSecond);
num +=15;
}
browser.close();
})();
それでは、どのようにこのエラーを解決し、どのように再帰的なサイクルでサイクルに変換?
これは解決策ではありません。ログは、スクリプトがエラーハンドラなしで拒否されてしまう約束を作成し、コードが予測不可能な状態になる可能性があるという事実から来ています。単純にtry/catchブロックで全体をラップするのは、無視したくないエラーを捕まえてしまうので、実際はかなり悪い考えです。約束拒否についてのガイダンスについては、[公式文書](https://nodejs.org/api/process.html#process_event_unhandledrejection)を参照してください。 – ClementParis016