For example, given a variable
var links = stew.select(dom,'a[href]');
will return an array of all the anchor tags (
<a>) found in
dom that include an
var metadata = stew.select(dom,'head meta[name=/^dc\.|:/i]');
will extract the Dublin Core metadata from a document by selecting every
<meta> tag found in the
<head> that has a
name attribute that starts with
DC: (ignoring case).
Stew is often used as a toolkit for "screen-scraping" web pages (extracting data from HTML and XML documents).
(The name "stew" is inspired by the Python library BeautifulSoup, Simon Willison's soupselect extension of BeautifulSoup, and Harry Fuecks' Node.js port of soupselect. Stew is a meatier soup.)
Read on for more information, or:
- visit the repository on GitHub.
- review the API.
- see a complete example of using Stew (in a "literate CoffeeScript" format).
- browse the annotated source code or test coverage report.
- learn how to contribute to Stew.
- see the version history and release notes.
(Links not working? Try it from heyrod.com/stew or view the "raw" files here.)
The source code and documentation for Stew is available on GitHub at rodw/stew. You can clone the repository via:
git clone email@example.com:rodw/stew.git
Stew is deployed as an npm module under the name
stew-select. Hence you can install a pre-packaged version with the command:
npm install stew-select
and you can add it to your project as a dependency by adding a line like:
devDependencies part of your
Core CSS Selectors
Stew supports the full Version 2.1 CSS selector syntax and much of Version 3, including
The universal selector (
stew.select( dom, '*' )selects all the tags in the document.
Type selectors (
stew.select( dom, 'h2' )selects all the
h2tags in the document.
Class selectors (
stew.select( dom, '.foo' )selects all tags in the document with the class
ID selectors (
stew.select( dom, '#foo' )selects all tags in the document with the id
Descendant selectors (
stew.select( dom, 'div h2 a' )selects all
atags with an
h2ancestor that has a
Child selectors (
E > F).
stew.select( dom, 'div > h2 > a')selects all
atags with an
h2parent that has a
Attribute name selectors (
stew.select( dom, 'a[href]')selects all
atags with an
stew.select( dom, '[href]')selects all tags with an
Attribute value selectors (
stew.select( dom, 'a[rel="author"]')selects all
atags with a
relattribute set to the value
stew.select( dom, 'a[class~="author"]')selects all
atags with the
author, whether or not that tag has other classes as well. More generally
~=treats the attribute value as a white-space delimited list of values (to which the given value is compared).
stew.select( dom, 'div[lang|="en"]')selects all
divtags with a
langattribute whose value is exactly
enor whose value starts with
stew.select( dom, 'a[href^="https://"]')selects all
atags with an
hrefattribute value that starts with
stew.select( dom, 'a[href$=".html"]')selects all
atags with an
hrefattribute value that ends with
stew.select( dom, 'a[href*="://heyrod.com/"]')selects all
atags with an
hrefattribute value that contains with
Adjacent selectors (
E + F).
stew.select( dom, 'h1 + p')selects all
ptags that immediately follow an
Preceeding sibling selectors (
E ~ F).
stew.select( dom, 'h1 ~ p')selects all
ptags that follow an
h1tag (even if there are other tags between the
The "or" conjunction (
stew.select( dom, 'h1, h2')selects all
The :first-child pseudo-class (
stew.select( dom, 'li:first-child' )selects all
litags that happen to be the first tag among its siblings.
And of course, you can use arbitrary combinations of these selectors:
stew.select( dom, 'article div.credits > a[rel=license]' ); stew.select( dom, 'h1, h2, h3, h4, h5, h6, .heading' ); stew.select( dom, 'h1.title + h2.subtitle' ); stew.select( dom, 'ul > li > a[rel=author][href]' );
Stew extends the CSS selector syntax by allowing the use of regular expressions to specify tag names, class names, ids, and attributes (both name and value).
var metadata = stew.select(dom,'a[href=/^https?:/i]');
will select all anchor (
<a>) tags with an
href attribute that starts with
https: (with a case-insensitive comparison).
Another example, the snippet:
var metadata = stew.select(dom,'[/^data-/]');
selects all tags with an attribute whose name starts with
Any name or value that starts and ends with
/ will be treated as a regular expression. (Or, more accurately, any name or value that starts with
/ and ends with
/ with an optional suffix of any combination of the letters
\b and other special class markers.
Here are some example CSS selectors using regular expressions:
- Tag names:
div, but also
- Class names:
./^nav/matches any tag with a class name that starts with the string
#/^main$/imatches any tag with the id
main, using a case insensitive comparison (so it also matches
Mainand other variants.
- Attribute names: As above,
[/^data-/]matches any tag with an attribute whose name starts with
- Attribute values: As above,
[href=/^https?:/i]matches any tag with an
hrefattribute whose value starts with
These may be used in any combination, and freely mixed with "regular" CSS selectors.
Stew currently has a couple of known issues that crop up during specific (and rare) edge-cases. We intend to eliminate these in future releases, but want to make you aware of them so that you're not surprised.
(Developers: If you'd like to help address these issues, we'd love your help. Feel free to submit a pull request or reach out for more information.)
CSS 3 Selectors aren't (yet) fully supported.
Our intention is to fully support the most recent CSS selector syntax.
Stew supports all of the CSS 2.1 Selectors. (To the extent that it makes sense to do so. It's hard to see how to interpret
:visited and so on when looking at static-HTML from the server side, although
:first-child is supported.)
Not quite all of the CSS 3 Selectors are supported. Currently certain structural pseudo-classes and pseduo-elements are not supported (yet).
Stew may not report all syntax errors.
Stew will accept and properly parse any valid CSS selectors (unless listed as limitation elsewhere in this section).
However, (currently) Stew does not always reject every invalid selector. In particular, Stew's parser may ignore the invalid parts of improperly formed selectors, which can lead to unexpected results.
Stew requires white-space around the "generalized sibling" operator:
E ~ F works, but
Stew parses most operators (including
,) with or without white-space. In other words, Stew treats the following selectors as equivalent:
E + F,
E , F,
E > F,
Unfortantely, due to a quirk of Stew's current parser, the same is not true for the "preceeding sibling" operator (
~). That is, Stew supports
E ~ F but does not properly parse
E~F. Currently the
~ character must be surrounded by white-space.
(If you're curious, the
~= operator is the complicating factor for
~ right now. The same patterns we use for
> don't quite work for
The Stew library and related documentation are made available under an MIT License. For details, please see the file MIT-LICENSE.txt in the root directory of the repository.