Javascript 变量在thenOpen函数中具有不同的值
我正在使用CasperJS构建一个web刮板。我正在努力刮去3页。每个页面有15个链接,我想刮 在下面的代码片段中,我要刮取的所有链接都收集在Javascript 变量在thenOpen函数中具有不同的值,javascript,casperjs,Javascript,Casperjs,我正在使用CasperJS构建一个web刮板。我正在努力刮去3页。每个页面有15个链接,我想刮 在下面的代码片段中,我要刮取的所有链接都收集在allLinks数组中。它的长度是45。当我记录阵列时,我会看到所有我想要正确刮取的链接。然后我迭代每个链接并访问页面。当我记录链接(包含每个链接)时,我仍然可以按预期看到所有45个链接。但是,当我在然后打开功能中记录链接时,我只看到第一页的链接。因此,它只记录了15个链接。它不会记录第2页和第3页的任何链接 casper.then(function(){
allLinks
数组中。它的长度是45。当我记录阵列时,我会看到所有我想要正确刮取的链接。然后我迭代每个链接并访问页面。当我记录链接
(包含每个链接)时,我仍然可以按预期看到所有45个链接。但是,当我在然后打开功能中记录链接
时,我只看到第一页的链接。因此,它只记录了15个链接。它不会记录第2页和第3页的任何链接
casper.then(function(){
this.each(allLinks,function(self,link){
console.log("Getting all the links that need to be visited");
console.log(allLinks);
console.log("Getting each link");
console.log(link);
this.thenOpen(link,function(a){
console.log("Inside function that extracts data");
console.log(link);
});
});
});
下面是完整的代码
var casper = require('casper').create();
var url = casper.cli.get(0);
console.log(url);
var page2 = casper.cli.get(1);
console.log(page2);
jsonObj = { data : [] };
//var url = 'http://www.houzz.com/professionals/c/Nashville--TN/p/15';
var webPage = require('webpage');
zapTitle = [];
zapContact = [];
zapServices = [];
var page = webPage.create();
var nextBtn = "a.navigation-button.next";
var allLinks = [];
casper.start(url);
casper.waitForSelector(nextBtn, processPage);
casper.run();
function processPage() {
for (var i = 1; i <= page2; i = i + 1) {
this.then(function(){
console.log(i);
var pageData = this.evaluate(getPageData);
allLinks = allLinks.concat(pageData);
console.log(allLinks);
if (!this.exists(nextBtn)) {
return;
}
this.thenClick(nextBtn).then(function() {
this.echo(this.getCurrentUrl());
});
});
};
}
function getPageData(){
//return document.title;
var links = document.getElementsByClassName('pro-title');
links = Array.prototype.map.call(links,function(link){
return link.getAttribute('href');
});
return links;
}
casper.then(function(){
this.each(allLinks,function(self,link){
console.log("Inside the each function");
console.log(link);
this.thenOpen(link,function(a){
console.log("Inside function that extracts data");
console.log(link);
var description = this.fetchText('div.profile-about div:nth-child(1)');
description = description.replace(/[\t\n]/g,"");
var name = this.getHTML('div.pro-info-horizontal-list div.info-list-label:nth-child(2) div.info-list-text');
name = name.replace(/[<b>Contact</b>: ]/g,"");
jsonObj.data.push({
"title" : this.fetchText('a.profile-full-name'),
"contact" : this.fetchText('div.profile-about div:nth-child(1)'),
"services" : this.getHTML('div.info-list-text span:nth-child(2) span'),
"name" : name,
"location" : this.getHTML('div.pro-info-horizontal-list div.info-list-label:nth-child(3) div.info-list-text span'),
"description" : description,
"reviews" : this.getHTML('div.pro-rating a span.pro-review-string span')
});
casper.open('https://zapier.com/hooks/catch/29s1m6/', {
method: 'post',
data: {
"title" : this.fetchText('a.profile-full-name'),
"contact" : this.getHTML('div.pro-contact-methods span.pro-contact-text:nth-child(2)'),
"services" : this.getHTML('div.info-list-text span:nth-child(2) span'),
"name" : name,
"location" : this.getHTML('div.pro-info-horizontal-list div.info-list-label:nth-child(3) div.info-list-text span'),
"description" : description,
"reviews" : this.getHTML('div.pro-rating a span.pro-review-string span')
}
});
}).then(function() {
console.log(jsonObj.data.length);
//console.log(jsonObj);
if (jsonObj.data.length == 13) {
console.log(jsonObj.data[13].title);
}
/*for(var i = 0; i < jsonObj.data.length; i = i + 1 ) {
console.log(i);
console.log("zaptitle");
//zapTitle.push(jsonObj.data[i]);
console.log(jsonObj.data[i].title);
//}
}*/
//require('utils').dump(jsonObj.data[2].title);
//require('utils').dump(jsonObj);
//require('utils').dump(jsonObj.data[8]);
//require('utils').dump(zapTitle);
for(var i = 0; i < jsonObj.data.length; i = i + 1 ) {
zapServices.push(jsonObj.data[i].services);
}
/*casper.open('https://zapier.com/hooks/catch/29s1m6/', {
method: 'post',
data: {"title" : zapTitle,
//"contact" : zapContact,
"services" : zapServices
}*/
});
});
});
var casper=require('casper').create();
var url=casper.cli.get(0);
console.log(url);
var page2=casper.cli.get(1);
控制台日志(第2页);
jsonObj={data:[]};
//var url='1〕http://www.houzz.com/professionals/c/Nashville--TN/p/15';
var webPage=require('webPage');
zapTitle=[];
zapContact=[];
zapServices=[];
var page=webPage.create();
var nextBtn=“a.navigation-button.next”;
var-allLinks=[];
casper.start(url);
casper.waitForSelector(下一页,流程页);
casper.run();
函数processPage(){
for(var i=1;icasper.getHTML(选择器)
失败,如果找不到要查找的元素(),则退出脚本。当然,如果使用的是PhantomJS 2.0或2.1,则会隐藏此错误
罪魁祸首是“location”:此.getHTML('div.pro-info-horizontal-list div.info-list-label:nth child(3)div.info-list-text span'),
,因为它不存在。在尝试访问它之前,需要检查选择器是否存在(例如,使用casper.exists(选择器)
)
这就是a所能完成的。我不明白这是怎么可能的。链接
是“关闭”的,所以它在中必须是相同的。然后打开(链接
和同时在控制台.log(链接)中
。也许你删除了一些可能是罪魁祸首的代码。链接
在控制台和控制台中是不一样的。日志
。请看一下我问题中的完整代码。@ArtjomB.看起来不错。我知道你用过。是的,我用过,但是链接
在控制台和控制台中是不一样的。日志
。这就是为什么我只有y能够从第一页提取数据。我看不到剩余两页中的任何数据。我将3作为参数传递,因此page2
的值为3。此外,由于您知道脚本停在哪个页上,您可以更仔细地查看它,以了解它与其他页的区别。