如何在每次迭代中传递变量时循环 Horseman 实例?
How to Loop Horseman instance while passing a variable in each iteration?
所以我正在尝试使用 horseman.js 在 javascript 中编写一个脚本,它将从存储在 [=53= 的数组中的每个 link 中提取所有 html ]s
基本思路如下,记录一个url的html就好了
var Horseman = require('node-horseman');
var result = "";
var pulledHtml = "";
var horseman = new Horseman();
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
console.log(results);
当我尝试循环这个时,问题就来了
例如
var result = "";
var pulledHtml = "";
var website = ["www.example1.com","www.example2.com"]; //(etc)
var horseman = new Horseman();
for (var i = 0; i < website.length; i++) {
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
}
console.log(result);
现在我知道这个循环注定要失败问题是我对horseman.js太陌生了,无法很好地掌握如何从上面的代码中解决这个问题
问题是:
1.) for 循环是同步的,而 horseman 是异步的,因此 for 循环在完成从当前 url
中拉出 html 之前再次调用 horseman
2.) 似乎无法确定如何最好地传递 html 我发现一个新变量,因为我确定我做得不好(最终目标是让所有 html保存在一个变量中)
到目前为止我尝试解决我的第一个问题的方法是
var chain = horseman
for(var i = 1; i < website.length; i++) {
chain = horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
}
但是我没有一个很好的方法来测试这个,因为 console.logging 在循环没有 return 任何东西之后的结果(我稍后会用结果变量做事)
但是,这似乎需要等待,因为 console.log 不会立即 return 一个空的结果变量,就像在注定的 for 循环示例中那样
最后我试过了
async.each(website, function(item, callback) {
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
callback();
});
console.log(result);
仍然无济于事,
如果有人能帮我解决这个问题,非常感谢!
我不认识这个 Horseman API,但因为它使用了 .then()
函数,所以我认为它是一个 Promise。
尝试这样做,
var result = "";
var website = ["www.example1.com","www.example2.com"]; //(etc)
var promises = [];
website.forEach(url => {
promises.push(new Horseman()
.open(url)
.html())
}
Promise.all(promises)
.then(results => {
return results.forEach(html => {
result += html;
})
})
.then(()=> {
promises.forEach(horse => {
horse.log().close()
})
console.log(result);
})
所以我正在尝试使用 horseman.js 在 javascript 中编写一个脚本,它将从存储在 [=53= 的数组中的每个 link 中提取所有 html ]s
基本思路如下,记录一个url的html就好了
var Horseman = require('node-horseman');
var result = "";
var pulledHtml = "";
var horseman = new Horseman();
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
console.log(results);
当我尝试循环这个时,问题就来了
例如
var result = "";
var pulledHtml = "";
var website = ["www.example1.com","www.example2.com"]; //(etc)
var horseman = new Horseman();
for (var i = 0; i < website.length; i++) {
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
}
console.log(result);
现在我知道这个循环注定要失败问题是我对horseman.js太陌生了,无法很好地掌握如何从上面的代码中解决这个问题
问题是: 1.) for 循环是同步的,而 horseman 是异步的,因此 for 循环在完成从当前 url
中拉出 html 之前再次调用 horseman2.) 似乎无法确定如何最好地传递 html 我发现一个新变量,因为我确定我做得不好(最终目标是让所有 html保存在一个变量中)
到目前为止我尝试解决我的第一个问题的方法是
var chain = horseman
for(var i = 1; i < website.length; i++) {
chain = horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
}
但是我没有一个很好的方法来测试这个,因为 console.logging 在循环没有 return 任何东西之后的结果(我稍后会用结果变量做事)
但是,这似乎需要等待,因为 console.log 不会立即 return 一个空的结果变量,就像在注定的 for 循环示例中那样
最后我试过了
async.each(website, function(item, callback) {
horseman
.open(website)
.html()
.then(function(html){
pulledHtml = html;
result += pulledhtml;
return result;
})
.log()
.close();
callback();
});
console.log(result);
仍然无济于事,
如果有人能帮我解决这个问题,非常感谢!
我不认识这个 Horseman API,但因为它使用了 .then()
函数,所以我认为它是一个 Promise。
尝试这样做,
var result = "";
var website = ["www.example1.com","www.example2.com"]; //(etc)
var promises = [];
website.forEach(url => {
promises.push(new Horseman()
.open(url)
.html())
}
Promise.all(promises)
.then(results => {
return results.forEach(html => {
result += html;
})
})
.then(()=> {
promises.forEach(horse => {
horse.log().close()
})
console.log(result);
})