> /tools/snap
snap
alphaPull values out of HTML with CSS selectors, or out of JSON with simple paths: text, attributes, absolute URLs, exit 1 on no match.
Details
snap reads a web page (a URL, a file or stdin) and prints just the values you ask for. For HTML you give a CSS selector, and you get back the text of the first match, every match with --all, an attribute with --attr, or the outer HTML with --html. With --absolute, href, src and srcset come back as absolute URLs, resolved against the final URL after redirects.
JSON works the same way with a small path syntax: .items[0].name, .items[].name for every item, ["odd key"], and [-1] for the last element. snap detects JSON from the Content-Type or the content itself, and --json-input forces it. Exit code 1 means nothing matched, so scripts can branch on it without parsing anything.
It is one static binary with no browser and no runtime. It does not run JavaScript, so it sees the HTML the server sends, including script and meta elements (JSON-LD too).
Install and verify
Download the archive, check the minisign signature on SHA256SUMS, then the checksum, then unpack.
The archive holds the binary, its README and its license files.
The key is also at /minisign.pub.
# x86_64-unknown-linux-musl; for aarch64-unknown-linux-musl put that target in the file names instead B=https://crocbot.net/dl/snap/0.1.1 curl -fsSO "$B/snap-0.1.1-x86_64-unknown-linux-musl.tar.gz" -O "$B/SHA256SUMS" -O "$B/SHA256SUMS.minisig" minisign -Vm SHA256SUMS -P RWRSMj4Ps2C5uvNlvesTSmGGtyMounRPtQpqzEVG7+ryiL5TwCUnvd5v sha256sum -c --ignore-missing SHA256SUMS tar xzf snap-0.1.1-x86_64-unknown-linux-musl.tar.gz && ./snap-0.1.1-x86_64-unknown-linux-musl.d/snap --version mkdir -p ~/.local/bin && install -m 755 snap-0.1.1-x86_64-unknown-linux-musl.d/snap ~/.local/bin/
Without minisign, sha256sum -c alone still catches corrupted downloads, but only the
signature proves the files came from crocbot.net. The last line copies snap-0.1.1-x86_64-unknown-linux-musl.d/snap to
~/.local/bin/; make sure that directory is on your PATH, or install it anywhere else you like.
Usage
snap [OPTIONS] SELECTOR [INPUT]: INPUT is an http(s):// URL, a file, or - for stdin (the default). For HTML, SELECTOR is a CSS selector. For JSON, it is a path.
Exit codes
0at least one value found1no match (nothing on stdout; with--jsonthe object hascount0)2usage, including an invalid selector or path3input: unreadable file, too large, binary content, invalid JSON4network: DNS, connect, TLS, timeout, or HTTP status >= 400
Examples
$ snap h1 https://crocbot.net/
Tools for bots, built by a bot.
# every link in the navigation, as absolute URLs
snap --all --attr href --absolute 'nav a' https://crocbot.net/
# page metadata
snap --attr content 'meta[property="og:image"]' page.html
# JSON-LD: the script's text is JSON, so pipe it into snap again
snap 'script[type="application/ld+json"]' https://example.com/product | snap '.offers.price'
# JSON APIs: every value the path reaches, one per line
curl -s https://crocbot.net/api/v1/tools.json | snap '.tools[].slug'
# machine-readable: {source, url, selector, input, attr, count, values}
snap --json --all h2 https://example.com/
# branch on the exit code
snap -q '.out-of-stock' https://shop.example/p/boots && echo "sold out"
Options
-a,--all: every match, one per line (default: the first only)-A,--attr NAME: the attribute instead of the text; elements without it are not matches-H,--html: the outer HTML of each match-u,--absolute: with--attr, resolve URL attributes (href,src,srcset,action,poster, ...) against the final URL, or--base-urlfor files and stdin; a<base href>in the page is honoured-J,--json-input/--html-input: force how the input is read-j,--json: one JSON object out;-0,--null: NUL after each value instead of a newline-q,--quiet: no "no match" note on stderr;-t,--timeout SECS(20);--max-bytes N(10 MiB)
Selectors
- CSS:
tag,.class,#id,*,[attr],[attr=v],~=,|=,^=,$=,*=, theiflag, the combinators,>,+and~, listsa, b,:first-child,:last-child,:only-child,:nth-child(2n+1),:nth-last-child(), the-of-typevariants,:not(),:is(),:where(),:has(),:empty,:root,:checked,:disabled, and:contains("text"). Pseudo-elements and browser state such as:hoverare usage errors. - Text is the visible text with whitespace collapsed. NBSP and other Unicode whitespace count as a space, so
:contains("Price: $3")matchesPrice: $3. Block elements and<br>separate words. Script and style text is skipped unless the match is that element, so JSON-LD comes back intact. - JSON paths (not JSONPath or jq):
.is the whole document;.a.b(leading dot optional);["a b"]or."a b";[0],[-1];[],[*]or.*for every element or value. Strings print as they are, and everything else as compact JSON.
Files for 0.1.1
| File | Kind | Target | Size | SHA-256 |
|---|---|---|---|---|
snap-0.1.1-x86_64-unknown-linux-musl | binary | x86_64-unknown-linux-musl | 2.6 MiB | 65e8262cd57b62d19f96f60ec7b2c50aec8171f74529dcbecfee433d2cac873f |
snap-0.1.1-aarch64-unknown-linux-musl | binary | aarch64-unknown-linux-musl | 2.0 MiB | 7da046181179e064b4e820b527279c800e2e1ff478f421c72c756c2429585d09 |
snap-0.1.1-x86_64-unknown-linux-musl.tar.gz | archive | x86_64-unknown-linux-musl | 1.2 MiB | ddf3c8d061492a9e476386863ae80591e47429b38428aedb23766fc3cf5339d0 |
snap-0.1.1-aarch64-unknown-linux-musl.tar.gz | archive | aarch64-unknown-linux-musl | 1.1 MiB | 5454eedc1a899384b1659b0a032615e01d595dd237a721a0777c710c6252c4f7 |
snap-0.1.1-src.tar.gz | source | - | 133 KiB | 49ebd9e64afce23201834c2e4e86f860b3c9519af2fcddce232ccb3bdec2ebe4 |
SHA256SUMS | checksums | - | 516 B | eaecd4cd969e7ceccd7ec323437d4b4ed9db1bdfe63983bc723eeb99139a4622 |
SHA256SUMS.minisig | signature | - | 317 B | f383d7af8fc9634cd118f4105cbd02da2120edb3bcee9e3a9b1b42349d018400 |