这是一个代码片段。过一会就卡住了。我需要为此添加超时:
await page.goto(url, {waitUntil: 'load', timeout: 0});
console.log("now here");
await page.waitForSelector('.classifiedDetailTitle');
console.log("wating");
await page.waitForSelector('.classifiedInfo ');
console.log("now waiting");
await page.waitForSelector('.classifiedInfoList');
console.log("waitign again");
await page.waitForSelector('.selected');
console.log("yahoo");
title = await page.evaluate(() => Array.from(document.querySelectorAll('.classifiedDetailTitle>h1'), element => element.textContent));
console.log("page eval");
//price = await page.evaluate(() => (document.querySelectorAll('.classifiedInfo >h3'), element => element.textContent));
price = await page.$$eval(".classifiedInfo >h3", paragraphs => paragraphs.map(p => p.innerText.trim()));
console.log("this page eval");
key = await page.$$eval(".classifiedInfoList>li>strong", paragraphs => paragraphs.map(p => p.innerText.trim()));
console.log("key");
value = await page.$$eval(".classifiedInfoList>li>span", paragraphs => paragraphs.map(p => p.innerText.trim()));
console.log("value");
//info = info.split("")
var dict = {}
features = await page.$$eval(".selected", paragraphs => paragraphs.map(p => p.innerText.trim()));
console.log("with features");
dict["features"] = features;
key.push("url");
key.push("title");
key.push("price");
value.push(url);
value.push(title);
value.push(price);
console.log(price);
for(let keys = 0; keys < key.length; keys++){
dict[key[keys]] = value[keys];
}
writer.write(dict);
如何让它在 n 秒后超时?它会使网页在一段时间后卡住,我必须手动关闭它才能继续运行。