Skip to content

Instantly share code, notes, and snippets.

@rpragana
Created September 28, 2017 16:42
Show Gist options
  • Save rpragana/496259781a2c8d3b453556f4a92d5a25 to your computer and use it in GitHub Desktop.
Save rpragana/496259781a2c8d3b453556f4a92d5a25 to your computer and use it in GitHub Desktop.
Web Scrape usando o NodeJS e o módulo Cheerio
#!/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment