Installations
npm install cheerio-advanced-selectors
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
9.6.1
NPM Version
5.7.1
Score
73.7
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
watson
Download Statistics
Total Downloads
2,907,134
Last Day
319
Last Week
1,207
Last Month
5,292
Last Year
158,113
GitHub Statistics
30 Stars
32 Commits
11 Forks
5 Watching
1 Branches
2 Contributors
Bundle Size
1.25 kB
Minified
617.00 B
Minified + Gzipped
Package Meta Information
Latest Version
2.0.1
Package Id
cheerio-advanced-selectors@2.0.1
Size
4.14 kB
NPM Version
5.7.1
Node Version
9.6.1
Publised On
12 Mar 2018
Total Downloads
Cumulative downloads
Total Downloads
2,907,134
Last day
-5.3%
319
Compared to previous day
Last week
-35.5%
1,207
Compared to previous week
Last month
-16.9%
5,292
Compared to previous month
Last year
-31.3%
158,113
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
cheerio-advanced-selectors
Add support for the following selectors to cheerio:
:first
:last
:eq(index)
More selectors can easily be added: Just open an issue and I'll look into it :)
This module is inspired by cheerio-eq with the added support for many different selectors.
Supports cheerio version 0.18.0 and above.
Installation
npm install cheerio-advanced-selectors
Usage
Use the .wrap()
function to make cheerio-advanced-selectors take care
of everything for you:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4cheerio = cheerioAdv.wrap(cheerio) 5 6var $ = cheerio.load('<div>foo</div><div>bar</div>') 7 8$('div:first').text() // => 'foo'
Advanced usage
Alternatively use the .find()
function to only use
cheerio-advanced-selectors for a specific selector:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4var $ = cheerio.load('<div><span>foo</span></div><div><span>bar</span></div>') 5 6cheerioAdv.find($, 'div:eq(1)').text() // => 'bar'
If you need to run the same selector on a lot of different HTML
documents, you can speed things up by pre-compiling the selector using
the .compile()
function:
1var cheerio = require('cheerio') 2var cheerioAdv = require('cheerio-advanced-selectors') 3 4var myH1 = cheerioAdv.compile('div:first span:eq(1) h1') 5 6var html1 = cheerio.load('<div><span><h1>foo1</h1></span><span><h1>bar1</h1></span></div>') 7var html2 = cheerio.load('<div><span><h1>foo2</h1></span><span><h1>bar2</h1></span></div>') 8 9myH1(html1).text() // => 'bar1' 10myH1(html2).text() // => 'bar2'
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:
1// this will not work: 2$('div:eq(1)'); 3 4// use this instead: 5$('div').eq(1);
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-advanced-selectors is here :)
Solution
The solution to the problem is to automatically parse the selector
string at run-time. So if you give cheerio-advanced-selectors 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:first span
will be converted and interpreted as
$('div').eq(1).find('h2').first().find('span')
.
Supported advanced selectors
This module currently only support a minimal subset of the possible advanced selectors:
:first
:last
:eq(index)
But don't fear :) It's easy to add support for other selectors. Just open an issue or make a pull request.
API
.wrap(cheerio)
Wraps the main cheerio module to overload the standard load
function
so it knows how to handle the advanced selectors.
Returns the cheerio
module.
.find(cheerio, selector [, context [, root]])
Run the selector
on the given cheerio object optionally within the
given context
and optionally on the given root
.
The cheerio
object is usually called $
.
.compile(selector)
Compiles the selector
and returns a function which take 3 arguments:
fn(cheerio [, context [, root]])
:
cheerio
- a reference to the cheerio object (usually called$
)context
- the context in which to run the selector (optional)root
- the HTML root on which to run the selector (optional)
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Score
3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More