I'm new to Node.js. I'm using zombie.js to scrape a web page title from a few websites. Below is my code:
var Browser = require("zombie");
var util = require("util");
halt = require('delayed');
title = [];
url = [ 'http://www.apple.com', 'http://www.microsoft.com', 'http://www.dell.com' ];
function getTitles(url){
//console.log('Start scraping title');
var length = url.length;
console.log('Total Site to Scrape: '+length);
label = 1;
for(var i=0;i<length;i++){
browser = new Browser()
browser.runScripts = false
browser.setMaxListeners(0);
browser.visit(url[i], function(e, browser, status, errors) {
browser.wait(function(){
title[i] = browser.text('html > head > title');
console.log(label+': '+title[i]);
browser.close();
label++;
});
});
};
}
getTitles(url);
halt.delay(function () {
console.log('Array Length: '+title.length)
console.log('Array Content: '+title)
}, 10)
Below is the output of the code:
Total Site to Scrape: 3
1: Apple
2: Dell Official Site - The Power To Do More | Dell
3: Microsoft Home Page | Devices and Services
Array Length: 4
Array Content: ,,,Microsoft Home Page | Devices and Services
The part that I don't understand:
- Why is the array length returned 4 instead of 3? There are only three urls
- Why is the array content only return the last element? Where are the other two missing elements?