htmlq

extracts data from HTML using CSS selectors

TLDR

Extract elements by selector
$ cat [page.html] | htmlq [.class-name]
Get attribute value
$ htmlq -a href [a.link] < [page.html]
Get text content
$ htmlq -t [p] < [page.html]
Pretty print HTML
$ htmlq -p [body] < [page.html]
Remove nodes before extracting
$ htmlq --remove-nodes [.unwanted] [div.content] < [page.html]
From URL via curl
$ curl -s [url] | htmlq [selector]

SYNOPSIS

htmlq [options] selector

DESCRIPTION

htmlq extracts data from HTML using CSS selectors. It's like jq for HTML, providing command-line HTML parsing.The tool reads HTML from stdin or files and outputs matching elements. It supports extracting text, attributes, and formatted HTML.

PARAMETERS

SELECTOR

CSS selector.
-a, --attribute ATTR
Only return this attribute's value from selected elements.
-t, --text
Output only the text content of selected elements.
-p, --pretty
Pretty-print the serialised HTML output.
-b, --base URL
Use this URL as the base for relative links.
-B, --detect-base
Detect the base URL from the document's `<base>` tag.
-f, --filename FILE
Input file (defaults to stdin).
-o, --output FILE
Output file (defaults to stdout).
-r, --remove-nodes SELECTOR
Remove matching nodes before output. May be specified multiple times.
-w, --ignore-whitespace
When printing text nodes, ignore whitespace-only nodes.
-h, --help
Display help information.
-V, --version
Show version information.

INSTALL

sudo pacman -S htmlq
sudo apk add htmlq
brew install htmlq
nix profile install nixpkgs#htmlq

CAVEATS

Does not validate HTML; malformed input may produce unexpected results. CSS selector support covers most common patterns but may not support all CSS4 pseudo-selectors. Input is read entirely into memory, so very large HTML documents may be slow.

HISTORY

htmlq was created as a command-line HTML querying tool inspired by jq for JSON processing.

SEE ALSO

pup(1), jq(1), xmllint(1), curl(1)