cheerio-eq
Add :eq()
selector functionality to cheerio.
If you are looking for a more advanced solution with support for multiple advanved selectors, check out the cheerio-advanced-selectors module.
What's the problem?
Cheerio sacrifices advanced CSS selector support for speed. This means
for instance that the :eq()
selector isn't supported. The work-around
is normally to use the .eq()
function instead:
// this will not work:; // use this instead:;
This is a good alternative if you write the CSS selectors from scrach,
but what if you are working with selectors that already contain :eq()
?
Don't fear, cheerio-eq is here :)
Solution
The solution to the problem is to automatically parse the selector
string at run-time. So if you give cheerio-eq a selector like
div:eq(1)
it will return the following cheerio cursor:
$('div').eq(1)
.
It also works for complex selectors, so that div:eq(1) h2:eq(0) span
will be converted and interpreted as
$('div').eq(1).find('h2').eq(0).find('span')
.
Installation
npm install cheerio-eq
Usage
var cheerio = ;var find = ; var html = '<div>foo</div><div>bar</div>';var selector = 'div:eq(1)'; var $ = cheerio; console; // => 'bar'
API
The cheerio-eq module exposes a single function, which takes 2 arguments:
- The cheerio DOM object (usually just named
$
) - A string containing the selector to parse
License
MIT