我尝试通过滚动(通过单击按钮)进行分页。但是当我想为这个行动添加一些延迟时,等待新的承诺' node在运行时给出错误如何从节点运行时转义此错误,这是代码片段,
let scrapedData = yield nightmare
.goto(nextExists)
.wait(3000)
.evaluate(function () {
var links = [];
var productList = "";
var flag = true;
while (flag) {
var element = document.querySelectorAll('.load-more');
console.log('aa ' + element[0].style.display == "");
if (element[0].style.display == "") {
element[0].click()
} else {
flag = false
}
await new Promise(resolve => setTimeout(resolve, 3000)); // error
/*
await new Promise(resolve => setTimeout(resolve, 3000));
^^^
SyntaxError: Unexpected token new
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
*/
}
console.log('all done')
})
答案 0 :(得分:0)
您在async
{。}}错过.evaluate(async function {})
个关键字。
以下是您要实现的目标的最小示例:
const Nightmare = require('Nightmare');
const nightmare = new Nightmare({show: true});
const scrapedData = nightmare
.goto('http://google.com')
// You can use `await` keyword only in `async` functions
// Async function *always* returns a promise which nightmare knows how to handle
.evaluate(async function () {
await new Promise(resolve => setTimeout(resolve, 3000));
})
.end()
.then(() => console.log('All done'));
您可以阅读有关async/await
here的更多信息。