Skip to content

Instantly share code, notes, and snippets.

@edvinasbartkus
Created February 2, 2014 22:37
Show Gist options
  • Save edvinasbartkus/8776042 to your computer and use it in GitHub Desktop.
Save edvinasbartkus/8776042 to your computer and use it in GitHub Desktop.
Node: fast HTML parsing with Stream
var request = require('minreq') || require('request'),
WritableStream = require('htmlparser2').WritableStream;
var count = 0;
var done = function () {
console.log('There are ' + count + ' script tags.');
process.exit(1);
};
var handlers = {
onopentag: function (name, attrs) {
if (name == "script") {
count += 1;
}
},
onend: done
};
var url = 'http://news.ycombinator.com';
var stream = new WritableStream(handlers, done);
request.get(url).pipe(stream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment