Skip to content

Instantly share code, notes, and snippets.

@diegok
Created July 14, 2015 23:26
Show Gist options
  • Save diegok/ff34be06fda166b727b0 to your computer and use it in GitHub Desktop.
Save diegok/ff34be06fda166b727b0 to your computer and use it in GitHub Desktop.
var Promise = require('promise'),
ua = require('request'),
cheerio = require('cheerio');
function http_get(url) {
return new Promise(function(resolve, reject) {
ua(url, function (err, res, body) {
if (err) { reject(err) }
else {
resolve({
body: body,
dom: cheerio.load( body, { normalizeWhitespace: true } ),
res: res,
success: function(){ return this.res.statusCode === 200 }
});
}
});
});
}
[
'https://soysuper.com/',
'http://google.com/',
'http://elpais.com/',
'http://publico.es/',
'http://eldiario.es',
'http://xxx.es',
'http://kupkup.com/'
].forEach(function(url){
http_get(url)
.then(function(tx){
if ( tx.success() ) { console.log( tx.dom('title').text() ) }
else { console.error("%s [%d]", url, tx.res.statusCode ) }
})
.catch(function(err){
console.error("%s [%s]", url, err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment