Last active
August 29, 2015 13:56
-
-
Save Integralist/9001300 to your computer and use it in GitHub Desktop.
Web Scraping with NodeJS (copied from http://www.storminthecastle.com/2013/08/25/use-node-js-to-extract-data-from-the-web-for-fun-and-profit/)
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
{ | |
"name": "WebScraping", | |
"main": "scrap.js", | |
"dependencies": { | |
"cheerio": "~0.13.1" | |
} | |
} |
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 http = require('http'); | |
function download(url, callback) { | |
http.get(url, function(res) { | |
var data = ''; | |
res.on('data', function(chunk) { | |
data += chunk; | |
}); | |
res.on('end', function() { | |
callback(data); | |
}); | |
}).on('error', function() { | |
callback(null) | |
}); | |
} | |
module.exports = download; |
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 cheerio = require('cheerio'); // converts string into dom tree and provides querying (and other) methods | |
var download = require('./scrap.js'); | |
download('http://www.integralist.co.uk/', function(data) { | |
if (data) { | |
var $ = cheerio.load(data) | |
$('h2').each(function(index, item) { | |
console.log($(item).text()); | |
}); | |
} | |
else console.log('error'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment