# cheerio [![Build Status](https://secure.travis-ci.org/cheeriojs/cheerio.svg?branch=master)](http://travis-ci.org/cheeriojs/cheerio)
Fast, flexible, and lean implementation of core jQuery designed specifically for the server.
## Introduction
Teach your server HTML.
```js
var cheerio = require('cheerio'),
$ = cheerio.load('
').text($(this).text());
}).html();
//=>
apple
orange
pear
```
#### .filter( selector )
.filter( selection )
.filter( element )
.filter( function(index) )
Iterates over a cheerio object, reducing the set of selector elements to those that match the selector or pass the function's test. When a Cheerio selection is specified, return only the elements contained in that selection. When an element is specified, return only that element (if it is contained in the original selection). If using the function method, the function is executed in the context of the selected element, so `this` refers to the current element.
Selector:
```js
$('li').filter('.orange').attr('class');
//=> orange
```
Function:
```js
$('li').filter(function(i, el) {
// this === el
return $(this).attr('class') === 'orange';
}).attr('class')
//=> orange
```
#### .first()
Will select the first element of a cheerio object
```js
$('#fruits').children().first().text()
//=> Apple
```
#### .last()
Will select the last element of a cheerio object
```js
$('#fruits').children().last().text()
//=> Pear
```
#### .eq( i )
Reduce the set of matched elements to the one at the specified index. Use `.eq(-i)` to count backwards from the last selected element.
```js
$('li').eq(0).text()
//=> Apple
$('li').eq(-1).text()
//=> Pear
```
#### .get( [i] )
Retrieve the DOM elements matched by the Cheerio object. If an index is specified, retrieve one of the elements matched by the Cheerio object:
```js
$('li').get(0).name
//=> li
```
If no index is specified, retrieve all elements matched by the Cheerio object:
```js
$('li').get().length
//=> 3
```
#### .end()
End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
```js
$('li').eq(0).end().length
//=> 3
```
#### .add( selector [, context] )
#### .add( element )
#### .add( elements )
#### .add( html )
#### .add( selection )
Add elements to the set of matched elements.
```js
$('.apple').add('.orange').length
//=> 2
```
### Manipulation
Methods for modifying the DOM structure.
#### .append( content, [content, ...] )
Inserts content as the *last* child of each of the selected elements.
```js
$('ul').append('
Plum')
$.html()
//=>
// - Apple
// - Orange
// - Pear
// - Plum
//
```
#### .prepend( content, [content, ...] )
Inserts content as the *first* child of each of the selected elements.
```js
$('ul').prepend('
Plum')
$.html()
//=>
// - Plum
// - Apple
// - Orange
// - Pear
//
```
#### .after( content, [content, ...] )
Insert content next to each element in the set of matched elements.
```js
$('.apple').after('
Plum')
$.html()
//=>
// - Apple
// - Plum
// - Orange
// - Pear
//
```
#### .before( content, [content, ...] )
Insert content previous to each element in the set of matched elements.
```js
$('.apple').before('
Plum')
$.html()
//=>
// - Plum
// - Apple
// - Orange
// - Pear
//
```
#### .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.
```js
$('.pear').remove()
$.html()
//=>
```
#### .replaceWith( content )
Replaces matched elements with `content`.
```js
var plum = $('
Plum')
$('.pear').replaceWith(plum)
$.html()
//=>
// - Apple
// - Orange
// - Plum
//
```
#### .empty()
Empties an element, removing all it's children.
```js
$('ul').empty()
$.html()
//=>
```
#### .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.
```js
$('.orange').html()
//=> Orange
$('#fruits').html('
Mango').html()
//=>
Mango
```
#### .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.
```js
$('.orange').text()
//=> Orange
$('ul').text()
//=> Apple
// Orange
// Pear
```
#### .css( [propertName] )
.css( [ propertyNames] )
.css( [propertyName], [value] )
.css( [propertName], [function] )
.css( [properties] )
Get the value of a style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.
### Rendering
When you're ready to render the document, you can use the `html` utility function:
```js
$.html()
//=>
// - Apple
// - Orange
// - Pear
//
```
If you want to return the outerHTML you can use `$.html(selector)`:
```js
$.html('.pear')
//=>
Pear
```
By default, `html` will leave some tags open. Sometimes you may instead want to render a valid XML document. For example, you might parse the following XML snippet:
```xml
$ = cheerio.load('
');
```
... and later want to render to XML. To do this, you can use the 'xml' utility function:
```js
$.xml()
//=>
```
### Miscellaneous
DOM element methods that don't fit anywhere else
#### .clone() ####
Clone the cheerio object.
```js
var moreFruit = $('#fruits').clone()
```
### Utilities
#### $.root
Sometimes you need to work with the top-level root element. To query it, you can use `$.root()`.
```js
$.root().append('
').html();
//=>
```
#### $.contains( container, contained )
Checks to see if the `contained` DOM element is a descendent of the `container` DOM element.
#### $.parseHTML( data [, context ] [, keepScripts ] )
Parses a string into an array of DOM nodes. The `context` argument has no meaning for Cheerio, but it is maintained for API compatability.
## Screencasts
http://vimeo.com/31950192
> This video tutorial is a follow-up to Nettut's "How to Scrape Web Pages with Node.js and jQuery", using cheerio instead of JSDOM + jQuery. This video shows how easy it is to use cheerio and how much faster cheerio is than JSDOM + jQuery.
## Test Coverage
Cheerio has high-test coverage, you can view the report [here](https://s3.amazonaws.com/MattMueller/Coverage/cheerio.html).
## Testing
To run the test suite, download the repository, then within the cheerio directory, run:
```shell
make setup
make test
```
This will download the development packages and run the test suite.
## Contributors
These are some of the contributors that have made cheerio possible:
```
project : cheerio
repo age : 2 years, 6 months
active : 285 days
commits : 762
files : 36
authors :
293 Matt Mueller 38.5%
133 Matthew Mueller 17.5%
92 Mike Pennisi 12.1%
54 David Chambers 7.1%
30 kpdecker 3.9%
19 Felix Böhm 2.5%
17 fb55 2.2%
15 Siddharth Mahendraker 2.0%
11 Adam Bretz 1.4%
8 Nazar Leush 1.0%
7 ironchefpython 0.9%
6 Jarno Leppänen 0.8%
5 Ben Sheldon 0.7%
5 Jos Shepherd 0.7%
5 Ryan Schmukler 0.7%
5 Steven Vachon 0.7%
4 Maciej Adwent 0.5%
4 Amir Abu Shareb 0.5%
3 jeremy.dentel@brandingbrand.com 0.4%
3 Andi Neck 0.4%
2 steve 0.3%
2 alexbardas 0.3%
2 finspin 0.3%
2 Ali Farhadi 0.3%
2 Chris Khoo 0.3%
2 Rob Ashton 0.3%
2 Thomas Heymann 0.3%
2 Jaro Spisak 0.3%
2 Dan Dascalescu 0.3%
2 Torstein Thune 0.3%
2 Wayne Larsen 0.3%
1 Timm Preetz 0.1%
1 Xavi 0.1%
1 Alex Shaindlin 0.1%
1 mattym 0.1%
1 Felix Böhm 0.1%
1 Farid Neshat 0.1%
1 Dmitry Mazuro 0.1%
1 Jeremy Hubble 0.1%
1 nevermind 0.1%
1 Manuel Alabor 0.1%
1 Matt Liegey 0.1%
1 Chris O'Hara 0.1%
1 Michael Holroyd 0.1%
1 Michiel De Mey 0.1%
1 Ben Atkin 0.1%
1 Rich Trott 0.1%
1 Rob "Hurricane" Ashton 0.1%
1 Robin Gloster 0.1%
1 Simon Boudrias 0.1%
1 Sindre Sorhus 0.1%
1 xiaohwan 0.1%
```
## Special Thanks
This library stands on the shoulders of some incredible developers. A special thanks to:
__• @FB55 for node-htmlparser2 & CSSSelect:__
Felix has a knack for writing speedy parsing engines. He completely re-wrote both @tautologistic's `node-htmlparser` and @harry's `node-soupselect` from the ground up, making both of them much faster and more flexible. Cheerio would not be possible without his foundational work
__• @jQuery team for jQuery:__
The core API is the best of its 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) 2012 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.