htmlq(1)
Extract data from HTML using CSS selectors, similar to jq for JSON.
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
| Flag | What it does |
|---|---|
-t, --text | Extract text content from matching elements instead of HTML |
-a, --attr ATTR | Extract the value of a specific attribute from matching elements |
-r, --remove SELECTOR | Remove elements matching this selector before extraction |
-p, --pretty | Pretty-print HTML output with proper formatting |
-b, --blank | Treat empty results as blank instead of producing no output |
-e, --eq INDEX | Select only the element at the specified index (0-based) |
-s, --select SELECTOR | Further filter results using an additional CSS selector |
--ignore-whitespace | Ignore 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 hrefExtract all h1 heading text from an HTML file
htmlq -t 'h1' page.htmlExtract only the first element matching 'div.post' selector
htmlq 'div.post' -e 0 page.htmlExtract author names but skip any inside elements with class 'deleted'
htmlq -t 'span.author' -r '.deleted' page.htmlExtract text from the third table cell in each row
htmlq -t 'table tr td' -e 2 page.htmlPretty-print all article elements to a new HTML file
htmlq -p 'article' page.html > output.htmlList unique image sources from a page
htmlq 'img' -a src page.html | sort -u