Web Scraping com o NodeJS
webscrape.js
#!/usr/bin/env node | |
var fs = require('fs'); | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
request('https://reddit.com/r/brasil', function (error, response, html) { | |
if (!error && response.statusCode == 200) { | |
var itens=[]; | |
var $ = cheerio.load(html); | |
$('.thing .top-matter').each(function(ix,elem){ | |
var item={}; | |
item.titulo = $(this).find('a.title').text() | |
item.url = $(this).find('a.title').attr('href') | |
item.hora = $(this).find('time').attr('title') | |
item.autor = $(this).find('a').eq(2).text() | |
itens[ix] = item | |
}) | |
fs.writeFile('redit_brasil.json', | |
JSON.stringify(itens,null,2),function(){ | |
console.log('Pronto!') | |
}) | |
} | |
}); | |
Web Scraping com o NodeJS
webscrape.js