transform streaming html using css selectors
The cornet is a brass instrument very similar to the trumpet, distinguished by its conical bore, compact shape, and mellower tone quality. - Wikipedia
This project is demonstrating how to use a couple of my libraries to replace
substack/node-trumpet in just a couple of LOC.
Even better, there are some advantages over
cornetworks as a handler for
fb55/htmlparser2, the probably fastest HTML parser currently available for node. And it's much less strict than the
saxmodule used by
MatthewMueller/cheeriomodule, you can do everything with your document that would be possible with jQuery.
Please note that callbacks are fired as soon as an element was retrieved. That means that no content past the element will be available, so cheerio won't find anything, and, as the element is at this time the last child of it's parent, selectors like
:nth-last-child won't work as expected.
npm install cornet
var Parser = require"htmlparser2"WritableStreamCornet = require"cornet"minreq = require"minreq"$ = require"cheerio";var cornet = ;minreqget""pipecornet;cornetremove"script"; //remove all scripts//show all reposcornetselect".repo_list"$elemfind"h3"eachconsole.log"repo %d: %s" i + 1 $thistexttrim;;;//does the samevar i = 0;cornetselect".repo_list h3"console.log"repo %d: %s" ++i $elemtexttrim;;//sometimes, you only want to get a single elementvar onTitle = cornetselect"title"console.log"Page title:" $titletexttrim;cornetremoveLister"element" onTitle;;
options are the same you can pass to
EventEmitter that emits two events:
elementis emitted whenever an element was added to the DOM.
domis emitted when the DOM is complete.
cornet#select(selector | fn, cb)
Calls the callback when the selector is matched or a passed function returns
true (or any value that evaluates to true).
Internally, listenes for any
element event and checks then if the selector is matched.
Returns the listening function, so you can remove it afterwards (as shown in the example above).
cornet#remove(selector | fn)
Removes all elements that match the selector. Also returns the listener.