$linuxjunkies
>

htmlq(1)

Extract data from HTML using CSS selectors, similar to jq for JSON.

UbuntuDebianFedoraArch

Synopsis

htmlq [OPTION]... SELECTOR [FILE]...

Description

htmlq is a command-line tool for parsing and extracting data from HTML documents using CSS selectors. It reads HTML from files or standard input and outputs matching elements or their attributes, making it useful for web scraping and HTML inspection in shell scripts.

Unlike traditional tools like grep or sed, htmlq understands HTML structure and selector syntax, allowing precise extraction of elements, text content, and attributes without fragile regex patterns.

Common options

FlagWhat it does
-t, --textExtract text content from matching elements instead of HTML
-a, --attr ATTRExtract the value of a specific attribute from matching elements
-r, --remove SELECTORRemove elements matching this selector before extraction
-p, --prettyPretty-print HTML output with proper formatting
-b, --blankTreat empty results as blank instead of producing no output
-e, --eq INDEXSelect only the element at the specified index (0-based)
-s, --select SELECTORFurther filter results using an additional CSS selector
--ignore-whitespaceIgnore leading/trailing whitespace in text extraction

Examples

Extract text content from a div with class 'title'

echo '<div class="title">Hello World</div>' | htmlq -t '.title'

Extract all href attributes from links on a webpage

curl https://example.com | htmlq 'a' -a href

Extract all h1 heading text from an HTML file

htmlq -t 'h1' page.html

Extract only the first element matching 'div.post' selector

htmlq 'div.post' -e 0 page.html

Extract author names but skip any inside elements with class 'deleted'

htmlq -t 'span.author' -r '.deleted' page.html

Extract text from the third table cell in each row

htmlq -t 'table tr td' -e 2 page.html

Pretty-print all article elements to a new HTML file

htmlq -p 'article' page.html > output.html

List unique image sources from a page

htmlq 'img' -a src page.html | sort -u

Related commands