第一部分:
目前我抓取一个网页来获取链接,然后我打开每个抓取的链接,这部分工作完美。
第二部分:
我检查是否有(select)选项字段的选择并将值(id)存储在数组中这部分也可以正常工作。
第三部分:
我想循环遍历它们(选择选项)并触发click事件并等待AJAX响应,然后提取infoProduct然后将其存储在数组中。
我在这一部分遇到麻烦我得到一个空数组,因为return listProducts
在第一个this.eachThen
开始之前被调用。
function getInfosProduct(obj,variation) {
if (!variation) {
return [{
name: getName(),
image: products.image.getElement.link + getImage(),
url: obj.url
}];
} else {
return {
name: getName(),
image: products.image.getElement.link + getImage(),
url: variation.url,
idVariation: variation.id,
descVariation: variation.description
};
}
}
function clickVariation(variation) {
if (variation.ok && variation.id != variation.ignore) {
chooseVariation(products.selector, variation.id);
return true;
}
return false;
}
casper.getInfosProducts = function(obj) {
if (obj.level == 0) {
return this.evaluate(getInfosProduct, obj,false);
} else {
listProducts = [];
this.eachThen(obj.levelVariation, function getInfosProducts(variation) {
isClick = this.evaluate(clickVariation, variation.data)
if (isClick) {
this.waitForSelectorTextChange('.selector', function() {
this.echo('The text on .selector has been changed.');
});
listProducts.push(this.evaluate(getInfosProduct, obj,variation.data));
}
});
return listProducts;
}
};
触发select元素上的change事件的函数
function chooseVariation(selector, valueToMatch) {
var select = document.querySelectorAll(selector),
found = false;
Array.prototype.forEach.call(select, function(opt, i) {
if (!found && opt.value.indexOf(valueToMatch) !== -1) {
select.selectedIndex = i;
found = true;
}
});
// dispatch change event in case there is some kind of validation
var evt = document.createEvent("UIEvents"); // or "HTMLEvents"
evt.initUIEvent("change", true, true);
select[0].dispatchEvent(evt);
}
这是主要的功能:
function startSraping(obj) {
casper.then(function switchAction() {
switch (obj.action) {
//......... some code ........
// iterating through array links and open pages
case "openLinks":
this.each(links, function eachOpenLinks(self, link) {
if (link.ok) {
self.thenOpen(link.url, function thenOpenLinks() {
startSraping({
url: link.url,
action: "getVariations"
});
});
}
});
break;
// get all variations for each page opend
case "getVariations":
objVariations = this.getVariations(obj.url);
startSraping({
url: obj.url,
action: "getInfosProducts",
objVariations: objVariations
});
break;
case "getInfosProducts":
this.eachThen(obj.objVariations.list, function(levelVariation) {
infosProd = this.getInfosProducts({
levelVariation: levelVariation.data,
url: obj.url,
level: obj.objVariations.level
});
// Here I got an empty array
this.echo(JSON.stringify(infosProd), 'INFO');
});
break;
}
});
}
casper.start(url, function start() {
startSraping({
variation: variation,
action: "submitSearch"
});
});
casper.run();
答案 0 :(得分:1)
您不能在一个函数内部调用异步函数(eachThen
和waitForSelectorTextChange
都是异步的),该函数应该以同步方式从异步函数返回结果({ {3}})。由于CasperJS不支持Promises,这有点棘手。
我认为以下更改应该是最小的,并让您到达目的地。
casper.getInfosProducts = function(obj, callback) {
if (obj.level == 0) {
this.then(function(){
callback.call(this, arr.push(this.evaluate(getInfosProduct, obj,false));
});
} else {
var listProducts = [];
this.eachThen(obj.levelVariation, function getInfosProducts(variation) {
var isClick = this.evaluate(clickVariation, variation.data)
if (isClick) {
this.waitForSelectorTextChange('.selector', function() {
this.echo('The text on .selector has been changed.');
listProducts.push(this.evaluate(getInfosProduct, obj, variation.data));
});
}
});
this.then(function(){
callback.call(this, listProducts);
});
}
};
在startSraping
:
case "getInfosProducts":
this.eachThen(obj.objVariations.list, function(levelVariation) {
this.getInfosProducts({
levelVariation: levelVariation.data,
url: obj.url,
level: obj.objVariations.level
}, function (infosProd){
// this is the asynchronous callback
this.echo(JSON.stringify(infosProd), 'INFO');
});
});
break;