monk command tool
last modified July 12, 2026
The monk command is a terminal tool for parsing HTML and applying
CSS selectors to select elements from HTML pages. It reads HTML from standard
input, a local file, or a URL, applies one or more CSS selectors, and prints the
matching nodes. This tutorial covers installation, usage, and practical examples
of monk.
monk is useful for web scraping, extracting data from HTML
documents, and inspecting page structure directly from the command line. It
supports standard CSS selectors plus several extensions for advanced element
matching.
Installation
The tool can be installed via go install:
$ go install github.com/janbodnar/monk@latest
Alternatively, it can be built from source:
$ git clone https://github.com/janbodnar/monk $ cd monk $ go build -o monk .
Help Output
The following is the output of the monk --help command.
$ monk --help
Usage
monk [flags] [selectors]
Version
0.1.0
Flags
-c --color print result with color
-f --file file to read from
-u --url URL to fetch HTML from
-i --indent number of spaces to use for indent or character
-n --number print number of elements selected
-l --limit restrict number of levels printed
-p --plain don't escape html
-r --raw raw output
--pre preserve preformatted text
--charset specify the charset for monk to use
--json output in JSON format
--text output as plain text
--attr <name> output the value of the specified attribute
-v --version display version
Flags
The following table lists the available flags.
| Flag | Description |
|---|---|
-c, --color | Print result with color |
-f, --file | File to read from |
-u, --url | URL to fetch HTML from |
-i, --indent | Number of spaces to use for indent or character |
-n, --number | Print number of elements selected |
-l, --limit | Restrict number of levels printed |
-p, --plain | Don't escape HTML entities |
-r, --raw | Raw output (no newlines between tags) |
--pre | Preserve preformatted text |
--charset | Specify the charset for monk to use |
--json | Output in JSON format |
--text | Output as plain text |
--attr <name> | Output the value of the specified attribute |
-v, --version | Display version |
Selectors
monk supports standard CSS selectors via the goquery library, plus several extensions.
| Selector | Description |
|---|---|
tag | Match elements by tag name |
#id | Match element by id attribute |
.class | Match elements by class name |
[attr] | Match elements that have the attribute |
[attr=val] | Match elements where attribute equals value |
a b | Descendant: match b anywhere inside a |
a > b | Child: match b that is a direct child of a |
a + b | Adjacent sibling: match b immediately after a |
sel1, sel2 | Union: match elements from either selector |
:nth-child(n) | Match element that is the nth child |
:first-child | Match the first child element |
:last-child | Match the last child element |
:contains("text") | Match elements with a direct text child containing text |
:matches("regex") | Match elements with a direct text child matching the regex |
:parent-of(sel) | Match elements that have a direct child matching sel |
head(n) | Keep only the first n matched elements |
tail(n) | Keep only the last n matched elements |
Select h1 Elements from a File
This example demonstrates how to select all h1 elements from an
HTML file.
$ monk -f page.html h1
The command reads the HTML file and prints all h1 elements as an indented tree.
Fetch URL and Select Title
This example shows how to fetch a web page and select the title element.
$ monk -u https://example.com title
With the -u flag, monk directly fetches the HTML from the given URL and applies the selector, without needing a separate tool like curl or xh.
Fetch URL and Select All Links
This example demonstrates selecting all hyperlinks from a remotely fetched page.
$ monk -u https://example.com a
Monk fetches the page and selects all a elements. The
-u flag makes it easy to scrape web pages directly from the command
line.
Pipe HTML and Select Paragraphs
This example demonstrates piping HTML content into monk.
$ curl -s https://example.com | monk p
The HTML is piped via standard input and all p elements are
selected. Alternatively, monk -u https://example.com p achieves the
same result without piping.
Select li Elements Inside ul
This example shows how to select all li elements that are
descendants of a ul element.
$ monk -f page.html "ul li"
The descendant selector matches li elements at any nesting level
inside ul.
Select Direct Children
This example demonstrates selecting elements that are direct children of a parent.
$ monk -f page.html "div > p"
The child selector (>) matches only p elements that are immediate children of a div.
Select Adjacent Sibling
This example shows how to select a paragraph immediately following an h2 element.
$ monk -f page.html "h2 + p"
The adjacent sibling selector matches the p that directly follows an h2.
Print Text Content
This example demonstrates printing only the text content of matched elements.
$ monk -f page.html --text p
The --text flag outputs the text content of all matched p elements without HTML markup.
Print Attribute Values
This example shows how to extract attribute values from matched elements.
$ monk -f page.html --attr href a
The --attr flag outputs the value of the href attribute for each matched a element.
Output as JSON
This example demonstrates outputting matched nodes in JSON format.
$ monk -f page.html --json ul
The --json flag formats the matched ul nodes as JSON, which is useful for programmatic processing.
Count Matched Elements
This example shows how to print the count of matched elements.
$ monk -f page.html -n li
The -n flag prints only the number of matched li elements instead of the elements themselves.
Limit Tree Depth
This example demonstrates limiting the depth of the output tree.
$ monk -f page.html -l 2 body
The -l 2 flag restricts the output to two levels of nesting within the body element.
Select Elements by Text Content
This example shows how to select elements containing specific text.
$ monk -f page.html 'li:contains("blue")'
The :contains() pseudo-selector matches li elements whose direct text child contains the word blue.
Select Elements by Regex
This example demonstrates selecting elements whose text matches a regular expression.
$ monk -f page.html 'li:matches("^g")'
The :matches() pseudo-selector matches li elements whose text starts with the letter g.
Limit Results with head and tail
This example shows how to keep only the first or last matched elements.
$ monk -f page.html "ul li head(3)" $ monk -f page.html "ul li tail(2)"
The head(n) extension keeps only the first n matched elements, while tail(n) keeps only the last n.
Multiple Selectors
This example demonstrates using multiple selectors at once.
$ monk -f page.html "h1, h2"
The comma-separated list matches all h1 and h2 elements from the document.
Select by ID
This example shows how to select elements by their id attribute.
$ monk -f page.html "#one p"
The #one selector matches the element with id="one", then selects all descendant p elements.
Select by Class
This example demonstrates selecting elements by class name.
$ monk -f page.html ".level-1 p"
The .level-1 selector matches elements with class="level-1", then selects all descendant p elements.
Color Output
This example shows how to print matched nodes with syntax highlighting.
$ monk -f page.html -c ul li
The -c flag enables colored output, making it easier to distinguish
HTML tags, attributes, and text.
Raw Output
This example demonstrates raw output without extra newlines.
$ monk -f page.html -r p
The -r flag outputs the matched nodes without extra newlines
between tags, producing a compact representation.
Source
In this article, we have explored various examples of using the monk
command for selecting HTML elements with CSS selectors, including text extraction, attribute retrieval, and JSON output.
Author
List all Linux tutorials.