Created
October 20, 2015 11:58
-
-
Save andantonyan/32a5ce2f042e16d1b862 to your computer and use it in GitHub Desktop.
scraping with node
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
'use strict;' | |
var Q = require('q'); | |
var fs = require('fs'); | |
var writeFileQ = Q.nfbind(fs.writeFile); | |
var rp = require('request-promise'); | |
var cheerio = require('cheerio'); | |
var SCRAPING_URI = 'https://www.google.com/?q=cats'; | |
var options = { | |
uri: SCRAPING_URI, | |
transform: function (body) { | |
return cheerio.load(body); | |
} | |
}; | |
return rp(options) | |
.then(function ($) { | |
var json = { | |
title: $('title').text() | |
} | |
return writeFileQ('output.json', JSON.stringify(json, null, 2)); | |
}) | |
.then(function () { | |
process.exit(0); | |
}) | |
.catch(function (err) { | |
console.log('Houston, we have a problem %s', err) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment