Last active
August 29, 2015 14:20
-
-
Save nrshrivatsan/9f0486d155c2d3c53acc to your computer and use it in GitHub Desktop.
Good and Bad parts of our dailyfood - powered by foodfacts.com. Input - Some Food you need every day in single-quotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var input = process.argv.slice(2); | |
var request = require('request'), cheerio = require('cheerio'); | |
req = request.defaults({ | |
jar : true, | |
rejectUnauthorized : false, | |
followAllRedirects: true | |
}); | |
function getContentAndScrape (url, selector) { | |
//Get Ingredients | |
req.get({ | |
url : url, | |
headers : { | |
'User-Agent' : 'Super Cool Browser' | |
} | |
}, function( err, resp, body ){ | |
var $ = cheerio.load(body); | |
$(selector).each( function (i,v) { | |
var itemURL = $(this).attr('href'); | |
; | |
req.get({ | |
url : itemURL, | |
headers : { | |
'User-Agent' : 'Super Cool Browser' | |
} | |
}, function( err, resp, body ){ | |
var $ = cheerio.load(body); | |
console.log("\n"+$('.pd-product-title').text()+"\n"); | |
//Good | |
var good = $('.pd-nutri-good'); | |
if(good){ | |
console.log('Good'); | |
good.each(function (i,v) { | |
console.log("\t"+$(this).text()); | |
}); | |
} | |
//Bad | |
var bad = $('.pd-nutri-bad'); | |
if(bad){ | |
console.log('Bad'); | |
bad.each(function (i,v) { | |
console.log("\t"+$(this).text()); | |
}); | |
} | |
} | |
); | |
}) | |
} | |
); | |
} | |
var url = "http://www.foodfacts.com/ci/nutritionsearch/"+input, selector = ".product-link > a"; | |
getContentAndScrape(url, selector); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment