使用 NightmareJS 时如何进行多个查询?
How can multiple queries be made when using NightmareJS?
下面的 Javascript 旨在使用 NightmareJS 搜索网站 3 post 秒和 return 上传者的用户名 post.
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var inputArray = [198,199,201];
var runNext = function (i) {
nightmare
.goto('http://theshitpit.com/goto.php')
.insert('form [name=postnum]', i)
.click('form [type=submit]')
.wait()
.evaluate(function () {
return document.querySelector('.username').innerHTML
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
}
var index = 0;
while(index<inputArray.length){
runNext(inputArray[index]);
index++;
}
出于某种原因,此代码在命令提示符中执行时输出以下内容:
Search failed {}
Search failed {}
我一直在努力理解为什么这不起作用。我已经尝试使用此代码(没有 while 循环)对特定 post 仅使用一次 运行,使用 runNext(inputArray[0])
并且效果很好。所以,当我尝试添加一个 while 循环来获取有关多个 post 的信息时,为什么它不起作用?
Nightmare 是异步的。发生错误是因为您一次循环调用 runNext
三次 - 而不是等待之前的搜索完成。
因此前两个搜索在开始后立即中断,只有最后一个有时间完成。
尝试在上一个搜索结束时启动下一个搜索:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var inputArray = [198, 199, 201];
var index = 0;
var runNext = function (i) {
nightmare
.goto('http://theshitpit.com/goto.php')
.insert('form [name=postnum]', inputArray[i])
.click('form [type=submit]')
.wait()
.evaluate(function () {
return document.querySelector('.username').innerHTML
})
.then(function (result) {
console.log(result);
})
.then(function(){
index++;
// We will only run bext search when we successfully got here
if(index < inputArray.length){
runNext(index);
} else {
console.log("End");
nightmare.halt();
}
})
.catch(function (error) {
console.error('Search failed:', error);
});
}
runNext(index);
下面的 Javascript 旨在使用 NightmareJS 搜索网站 3 post 秒和 return 上传者的用户名 post.
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var inputArray = [198,199,201];
var runNext = function (i) {
nightmare
.goto('http://theshitpit.com/goto.php')
.insert('form [name=postnum]', i)
.click('form [type=submit]')
.wait()
.evaluate(function () {
return document.querySelector('.username').innerHTML
})
.end()
.then(function (result) {
console.log(result)
})
.catch(function (error) {
console.error('Search failed:', error);
});
}
var index = 0;
while(index<inputArray.length){
runNext(inputArray[index]);
index++;
}
出于某种原因,此代码在命令提示符中执行时输出以下内容:
Search failed {}
Search failed {}
我一直在努力理解为什么这不起作用。我已经尝试使用此代码(没有 while 循环)对特定 post 仅使用一次 运行,使用 runNext(inputArray[0])
并且效果很好。所以,当我尝试添加一个 while 循环来获取有关多个 post 的信息时,为什么它不起作用?
Nightmare 是异步的。发生错误是因为您一次循环调用 runNext
三次 - 而不是等待之前的搜索完成。
因此前两个搜索在开始后立即中断,只有最后一个有时间完成。
尝试在上一个搜索结束时启动下一个搜索:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
var inputArray = [198, 199, 201];
var index = 0;
var runNext = function (i) {
nightmare
.goto('http://theshitpit.com/goto.php')
.insert('form [name=postnum]', inputArray[i])
.click('form [type=submit]')
.wait()
.evaluate(function () {
return document.querySelector('.username').innerHTML
})
.then(function (result) {
console.log(result);
})
.then(function(){
index++;
// We will only run bext search when we successfully got here
if(index < inputArray.length){
runNext(index);
} else {
console.log("End");
nightmare.halt();
}
})
.catch(function (error) {
console.error('Search failed:', error);
});
}
runNext(index);