Skip to content

Instantly share code, notes, and snippets.

@alanhoff
Created June 2, 2014 19:28
Show Gist options
  • Select an option

  • Save alanhoff/abba3ff15c5c2c3324c8 to your computer and use it in GitHub Desktop.

Select an option

Save alanhoff/abba3ff15c5c2c3324c8 to your computer and use it in GitHub Desktop.
var request = require('request');
var cheerio = require('cheerio');
// use [] ao invés de Array()
var posts = [];
// Iniciando o request principal
request('http://www.hardmob.com.br/promocoes', function teste(error, response, html) {
if (error || response.statusCode !== 200)
throw error | new Error('status code ', + response.statusCode);
var $ = cheerio.load(html);
$('h3.threadtitle a').each(function(i, e){
// Use {} ao invés de Obect()
var post = {
url : $(this).attr('href'),
titulo : $(this).text()
};
posts.push(post);
});
// Todas as threads foram carregadas, hora de
// fazer o parse de cada uma
parseThreads();
});
var parseThreads = function(){
var it = posts.slice(0);
var loop = function(){
if(!it.length)
return finalizar(); // Todos itens foram iterados
request.get(it.pop().url, function(err, res, body){
if(err)
throw err;
if(res.statusCode !== 200)
throw new Error('statusCode ' + res.statusCode)
// Popular a array original do jeito que você quiser...
posts[it.length + 1].html = body
// Chamar o loop novamente
loop()
});
}
loop(); // Iniciamos o primeiro loop
};
var finalizar = function(){
console.log('Finalizamos!');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment