我正在使用CasperJS构建一个Web scraper。我想刮3页。每个页面都有15个我想要删除的链接。
在下面的代码片段中,我想要抓取的所有链接都收集在allLinks
数组中。它的长度是45.当我记录数组时,我看到了我想要正确刮去的所有链接。然后我遍历每个链接并访问该页面。当我记录link
(包含每个链接)时,我仍然可以按预期看到所有45个链接。但是,当我在link
函数中记录thenOpen
时,我只看到第一页的链接。因此,它只记录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
}*/
});
});
});
答案 0 :(得分:0)
casper.getHTML(selector)
失败并退出脚本,如果它找不到您要查找的元素(code reference)。当然,如果您使用的是PhantomJS 2.0或2.1,则会隐藏此错误。
12th page上的罪魁祸首是"location" : this.getHTML('div.pro-info-horizontal-list div.info-list-label:nth-child(3) div.info-list-text span'),
,因为它并不存在。在尝试访问选择器之前,您需要检查选择器是否存在(例如,使用casper.exists(selector)
)。
这是little console.log
debugging可以完成的事情。