Created
June 2, 2014 19:28
-
-
Save alanhoff/abba3ff15c5c2c3324c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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