This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
/*
Function : To find the business with the most recommendations and return the business name and number of recommendations.
Author : Jaspreet Kaur
Date : 28-03-2017
*/
console.log("--Script Start--");
var request=require('request');
var cheerio = require('cheerio');
request({
method: 'GET',
url: 'http://www.homeimprovementpages.com.au/search/arborists/nsw/wollongong'
}, function(err, response, body) {
//If there is error log the error
if (err) return console.error(err);
// Tell Cherrio to load the HTML
var $ = cheerio.load(body);
//Declare variables
var recommendedName='';
var tempMsxRecommendation=0;
var arrAaxRecomenttionName=[];
var currentCount;
var nodeItem;
var currentCountElement;
//Loop to go through each basic listing div
$('div.basic-listing').each(function(i, element){
nodeItem = $(this);
currentCountElement = nodeItem.find('.basic-listing__recommendation-count');
//Check if the current element is not undefined
if(currentCountElement!=undefined){
currentCount = currentCountElement.text().replace(' Recommendations','');
recommendedName = nodeItem.find('.basic-listing__title').text();
//Check if the new recommendation is greater or equal to the current count
if(parseInt(currentCount)>parseInt(tempMsxRecommendation)){
tempMsxRecommendation=currentCount;
arrAaxRecomenttionName = [];
addItem(recommendedName,tempMsxRecommendation);
}
else if(parseInt(currentCount)==parseInt(tempMsxRecommendation)){
addItem(recommendedName,tempMsxRecommendation);
}
}
});
//Funtion to add the recommended business name and count to array
function addItem(name,count){
var maxItem={};
maxItem.name= name;
maxItem.count= count;
arrAaxRecomenttionName.push(maxItem);
}
console.log('Heighest Reccomendations Object in Json' , arrAaxRecomenttionName);
});