What is cheerio-select?
The cheerio-select npm package is a library that allows users to use CSS selectors to select and manipulate HTML elements within a cheerio instance. It is designed to work with the cheerio library, which is a fast, flexible, and lean implementation of core jQuery designed specifically for the server.
What are cheerio-select's main functionalities?
Selecting elements
This feature allows you to select HTML elements using CSS selectors. In the code sample, we select elements with the class 'apple' and log their text content.
const cheerio = require('cheerio');
const select = require('cheerio-select');
const html = '<ul id="fruits"><li class="apple">Apple</li><li class="orange">Orange</li></ul>';
const $ = cheerio.load(html);
const elements = select('.apple', $);
console.log($(elements).text()); // 'Apple'
Filtering elements
This feature allows you to filter a set of elements down to those that match a CSS selector. In the code sample, we filter a list of 'li' elements to only those with the class 'apple'.
const cheerio = require('cheerio');
const select = require('cheerio-select');
const html = '<ul id="fruits"><li class="apple">Apple</li><li class="orange">Orange</li></ul>';
const $ = cheerio.load(html);
const fruits = $('li').toArray();
const apples = select.filter('.apple', fruits, $);
console.log($(apples).text()); // 'Apple'
Other packages similar to cheerio-select
jsdom
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. It is more comprehensive than cheerio-select as it simulates a web browser's environment, allowing for dynamic content execution, but it is also heavier and slower.
jquery
Although jQuery is primarily used in the browser, it can be used in Node.js with a window provided by a package like jsdom. jQuery offers a wide range of features for DOM manipulation and traversal, similar to cheerio-select, but it is not as optimized for server-side usage.
puppeteer
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is typically used for browser automation, testing, and scraping. Unlike cheerio-select, Puppeteer operates on a full browser and is capable of rendering JavaScript, making it more powerful but also more resource-intensive.
cheerio-select
Tiny wrapper around FB55's excellent CSSselect library.
cheerio-select provides a comprehensive test suite based on sizzle's test suite. If all the tests pass in cheerio-select, then sizzle and cheerio-select should have identical functionality.
Usage
var select = require('cheerio-select'),
parse = require('cheerio').parse,
dom = parse('<ul id = "fruits"><li class = "apple">Apple</li></ul>');
select('#fruits > .apple', dom);
=> [{...}]
TODO
- Get all the unit tests to pass!
- Consider adding client-side tests
Run tests
npm install
make test
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.