nightmare.js,在 .evaluate 这让我在节点上出错
nightmare.js, on .evaluate this gives me an error on node
我正在尝试通过滚动(通过单击按钮)来分页。但是当我想用 'await new Promise' 节点为该操作添加一些延迟时,节点在 运行 时间给出了一个错误,如何从节点 运行 时间逃脱这个是代码片段,
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')
})
您在此处缺少 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 的更多信息。
我正在尝试通过滚动(通过单击按钮)来分页。但是当我想用 'await new Promise' 节点为该操作添加一些延迟时,节点在 运行 时间给出了一个错误,如何从节点 运行 时间逃脱这个是代码片段,
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')
})
您在此处缺少 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 的更多信息。