Node.js NodeJS:如何使用jsdom在瀑布中编写异步每个循环
我正在NODEJS上使用jsdom进行web抓取。我尝试了很多方法,比如async.queue、series等。我完全想找到解决方案,我知道我终于找到了,但不知道如何获取所有模型数据 让我们假设一下,我想把所有类别和它们的产品都删掉 类别->产品-->型号Node.js NodeJS:如何使用jsdom在瀑布中编写异步每个循环,node.js,Node.js,我正在NODEJS上使用jsdom进行web抓取。我尝试了很多方法,比如async.queue、series等。我完全想找到解决方案,我知道我终于找到了,但不知道如何获取所有模型数据 让我们假设一下,我想把所有类别和它们的产品都删掉 类别->产品-->型号 Mobile (Category) url is cat/mb Iphone (product) url is cat/mb/iphone Iphone 4 (mo
Mobile (Category) url is cat/mb
Iphone (product) url is cat/mb/iphone
Iphone 4 (model) url is cat/mb/iphone/iphone-4
Iphone 5 (model) url is cat/mb/iphone/iphone-5
Laptop (Category) url is cat/lp
Sony (product) url is cat/lp/sony
F Series (model) url is cat/lp/sony/fseries
E Series (model) url is cat/lp/sony/eseries
概念是,获取所有类别及其产品和模型信息数据
预期产出:
但是在我的代码中,由于瀑布中的每个循环,我丢失了太多的模型数据,让我们假设,没有获得所有的模型信息
我得到的结果如下:
主要代码如下所示
你可以用promise和q
按顺序写东西很容易
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();
什么是catEndpointUrl和分页_值?你能不能也发布这些数据…@Alexander catEndpointUrl表示类别的终点,比如说cat/mb,cat/lp。分页_值意味着假设我有索尼的产品型号,可能是1000个型号。因此,我将迭代每个页面并获取模型信息,例如cat/mb/iphone/paginate-1、cat/mb/iphone/paginate-2等等。好,说到要点,我想用aysnc模式迭代下面的每个循环。我怎么能那样做。$'formmodelcompare tablelistable tr'.eachfunction{………};我想用aysnc模式迭代下面的每个循环,而不影响异步瀑布,我该怎么做呢。$'formmodelcompare tablelistable tr'.eachfunction{………};
F series
async.each(pagination_values, function(paginateNum, callback) {
var endpointUrl = catEndpointUrl + '&order=panel_id&by=desc&page='+paginateNum;// 'http://www.panelook.com/appmodlist.php?order=panel_id&by=desc&applications[]=CNS&page='+paginateNum;
var baseurl = 'http://www.panelook.com/';
logger.info('Step 4: Product Pagination '+ paginateNum + ' - ' + endpointUrl);
async.waterfall([
function(callback) {
helperObj.fetchPageContent(endpointUrl, function(html) {
logger.info('Step 5: Product URL '+ endpointUrl);
callback(null, html);
});
},
function(html, callback) {
env({html:html,
done:function (err, window) {
if(err)
console.log(err.message);
var productList =[]
var $ = require('jquery')(window);
//............the below each function is not async i guess, Therefore, i am not getting all the models data..what to do ?...........
//
$('form#modelcompare table#listable tr').each(function() {
if($(this).find('td:nth-child(2)').text() != ''){
logger.info('All the data ' + $(this).find('td:nth-child(2) a').attr('href'));
}
});
callback();
}
});
}
], function(err, results) {
if(err)
console.log(err.message);
});
}, function(err) {
if(err)
console.log(err.message);
});
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();