cheerio
Tiny, fast, and elegant implementation of core jQuery designed specifically for the server.
Introduction
Teach your server HTML.
var cheerio = require("cheerio"),
$ = cheerio.load("<h2 class = 'title'>Hello world</h2>")
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
$.html()
=> <h2 class = "title welcome">Hello there!</h2>
Installation
npm install cheerio
... or to install the package globally:
npm install -g cheerio
Features
❤ Familiar syntax:
Cheerio implements a subset of core jQuery. Cheerio removes all the DOM inconsistencies and browser cruft from the jQuery library, revealing a truly gorgeous API.
ϟ Blazingly fast:
Cheerio works with a very simple, consistent DOM model. As a result parsing, manipulating, and rendering are incredibly efficient. Preliminary end-to-end benchmarks suggest that cheerio is about 8x faster than JSDOM.
❁ Insanely flexible:
Cheerio wraps around @tautologistics forgiving htmlparser. Cheerio can parse nearly any page or chunk of html or xml.
What about JSDOM?
I wrote cheerio because I found myself increasingly frustrated with JSDOM. For me, there were three main sticking points that I kept running into again and again:
• JSDOM's built-in parser is too strict:
JSDOM's bundled HTML parser cannot handle many popular sites out there today.
• JSDOM is too slow:
Parsing big websites with JSDOM has a noticeable delay.
• JSDOM feels too heavy:
The goal of JSDOM is to provide an identical DOM environment as what we see in the browser. I never really needed all this, I just wanted a simple, familiar way to do HTML manipulation.
API
Markup example we'll be using:
This is the HTML markup we will be selecting, traversing, and manipulating in all of the API examples.
<ul id = "fruits">
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
</ul>
Loading
First you need to load in the HTML. This step in jQuery is implicit, since jQuery operates on the one, baked-in DOM. With Cheerio, we need to pass in the HTML document.
This is the preferred method:
var cheerio = require('cheerio'),
$ = cheerio.load( '<ul id = "fruits">...</ul>' )
Optionally, you can also load in the HTML by passing the string as the context:
$ = require('cheerio')
$('ul', '<ul id = "fruits">...</ul>')
Or as the root:
$ = require('cheerio')
$('li', 'ul', '<ul id = "fruits">...</ul>')
Selectors
Cheerio's selector implementation is nearly identical to jQuery's, so the API is very similar.
$( selector, [context], [root] )
selector
searches within the context
scope which searches within the root
scope. selector
and context
can be an string expression, DOM Element, array of DOM elements, or cheerio object. root
is typically the HTML document string.
This selector method is the starting point for traversing and manipulating the document. Like jQuery, it's the primary method for selecting elements in the document, but unlike jQuery it's built on top of the soup-select library, not the Sizzle engine.
Examples:
$(".apple", '#fruits').text()
=> Apple
$('ul .pear').attr('class')
=> pear
$('li[class=orange]').html()
=> <li class = "orange">Orange</li>
See http://api.jquery.com/jQuery/ for more information
Attributes
Methods for getting and modifying attributes.
.attr( name, value )
Method for getting and setting attributes. May also use a map
and function
like jQuery. Gets the attribute value for only the first element in the matched set. If you set an attribute's value to null
, you remove that attribute.
$('ul').attr('id')
=> fruits
$('.apple').attr('id', 'favorite').html()
=> <li class = "apple" id = "favorite">Apple</li>
See http://api.jquery.com/attr/ for more information
.removeAttr( name )
Method for removing attributes by name
.
$('.pear').removeAttr('class').html()
=> <li>Pear</li>
.hasClass( className )
Check to see if any of the matched elements have a given className
.
$('.pear').hasClass('pear')
=> true
$('apple').hasClass('fruit')
=> false
$('li').hasClass('pear')
=> true
addClass( className )
Adds class(es) to all of the matched elements. Also accepts a function
like jQuery.
$('.pear').addClass('fruit').html()
=> <li class = "pear fruit">Pear</li>
$('.apple').addClass('fruit red').html()
=> <li class = "apple fruit red">Apple</li>
See http://api.jquery.com/addClass/ for more information.
removeClass( [className] )
Removes one or more space-separated classes from the selected elements. If no className
is defined, all classes will be removed. Also accepts a function
like jQuery.
$('.pear').removeClass('pear').html()
=> <li class = "">Pear</li>
$('.apple').addClass('red').removeClass().html()
=> <li class = "">Apple</li>
See http://api.jquery.com/removeClass/ for more information.
Traversing
.parent()
Gets the parent of the first selected element.
$('.pear').parent().attr('id')
=> fruits
.next()
Gets the next sibling thats an element of the first selected element.
$('.apple').next().hasClass('orange')
=> true
.prev()
Gets the previous sibling thats an element of the first selected element.
$('.orange').prev().hasClass('apple')
=> true
.siblings()
Gets the first selected element's siblings. Excludes itself.
$('.pear').siblings().length
=> 2
.children()
Gets the children of the first selected element.
$('#fruits').children().length
=> 3
.each( function(index, element) )
Iterates over a cheerio object, executing a function for each matched element. When the callback is fired, the function is fired in the context of the DOM element, so this
refers to the current element, same as element
.
var fruits = [];
$('li').each(function(i, elem) {
fruits[i] = $(this).text();
});
fruits.join(', ');
=> Apple, Orange, Pear
Manipulation
Methods for modifying the DOM structure.
.append( content, [content, ...] )
Inserts content as the last child of each of the selected elements.
$('ul').append('<li class = "plum">Plum</li>')
$.html()
=> <ul id = "fruits">
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
<li class = "plum">Plum</li>
</ul>
.prepend( content, [content, ...] )
Inserts content as the first child of each of the selected elements.
$('ul').prepend('<li class = "plum">Plum</li>')
$.html()
=> <ul id = "fruits">
<li class = "plum">Plum</li>
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
</ul>
.after( content, [content, ...] )
Insert content next to each element in the set of matched elements.
$('.apple').after('<li class = "plum">Plum</li>')
$.html()
=> <ul id = "fruits">
<li class = "apple">Apple</li>
<li class = "plum">Plum</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
</ul>
.before( content, [content, ...] )
Insert content previous to each element in the set of matched elements.
$('.apple').before('<li class = "plum">Plum</li>')
$.html()
=> <ul id = "fruits">
<li class = "plum">Plum</li>
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
</ul>
.remove( [selector] )
Removes the set of matched elements from the DOM and all their children. selector
filters the set of matched elements to be removed.
$('.pear').remove()
$.html()
=> <ul id = "fruits">
<li class = "plum">Plum</li>
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
</ul>
.empty()
Empties an element, removing all it's children.
$('ul').empty()
$.html()
=> <ul id = "fruits"></ul>
.html( [htmlString] )
Gets an html content string from the first selected element. If htmlString
is specified, each selected element's content is replaced by the new content.
$('.orange').html()
=> <li class = "orange">Orange</li>
$('ul#fruits').html('<li class = "mango">Mango</li>').html()
=> <ul id="fruits">
<li class="mango">Mango</li>
</ul>
.text( [textString] )
Get the combined text contents of each element in the set of matched elements, including their descendants.. If textString
is specified, each selected element's content is replaced by the new text content.
$('.orange').text()
=> Orange
$('ul').text()
=> Apple
Orange
Pear
Rendering
When you're ready to render the document, you can use html
utility function:
$.html()
=> <ul id = "fruits">
<li class = "apple">Apple</li>
<li class = "orange">Orange</li>
<li class = "pear">Pear</li>
</ul>
If you want to render just a piece of the document you can use selectors:
$('.pear').html()
=> <li class = "pear">Pear</li>
Miscellaneous
DOM element methods that don't fit anywhere else
.get( [index] )
Retrieve the DOM elements matched by the cheerio object. If no index is specified, it will get an array of all matched elements.
$('li').get(0)
=> { raw: 'li class="apple"', ... }
$('li').get()
=> [ {...}, {...}, {...} ]
.size()
Return the number of elements in the cheerio object. Same as length
.
$('li').size()
=> 3
.toArray()
Retrieve all the DOM elements contained in the jQuery set, as an array.
$('li').toArray()
=> [ {...}, {...}, {...} ]
Utilities
$.dom()
Get the raw DOM of the parsed HTML document.
$.dom()
=> [ { raw: 'ul id="fruits"',
data: 'ul id="fruits"',
type: 'tag',
name: 'ul',
attribs: { id: 'fruits' },
children:
[ [Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object] ],
parent: null,
prev: null,
next: null } ]
$.isArray( array )
Checks to see the passed argument is an array.
$.isArray( $.dom() )
=> true
$.inArray( elem, arr )
Checks to see if the element is in the array
$.makeArray( obj )
Turns an array-like object (like $) into a native array.
$.each( obj, function(index, elem) )
Generic iterator function.
$.merge( one, two )
Merge the contents of two arrays together into the first array.
Special Thanks
This library stands on the shoulders of some incredible developers. A special thanks to:
• @tautologistics' node-htmlparser:
This HTML parser can parse anything! The parser produces really consistent results, even when the HTML string has errors. This man is a genius.
• @harryf's node-soupselect:
What an incredibly fast and precise CSS selector engine. Never really liked the feature-rich selector engines – this engine strikes a great balance.
• @jQuery team:
The core API is the best of it's class and despite dealing with all the browser inconsistencies the code base is extremely clean and easy to follow. Much of cheerio's implementation and documentation is from jQuery. Thanks guys.
• @visionmedia:
The style, the structure, the open-source"-ness" of this library comes from studying TJ's style and using many of his libraries. This dude consistently pumps out high-quality libraries and has always been more than willing to help or answer questions. You rock TJ.
License
(The MIT License)
Copyright (c) 2011 Matt Mueller <mattmuelle@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.