Configs2026年9月11日·1 分钟阅读

htmlq — Like jq but for HTML, Using CSS Selectors

Extract content from HTML files on the command line using CSS selectors, built in Rust for speed and simplicity.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
htmlq
先审查命令
npx -y tokrepo@latest install baff9069-ade5-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

htmlq is a command-line tool that lets you extract data from HTML using CSS selectors, the same way jq works for JSON. Written in Rust, it reads HTML from stdin and prints matching elements, attributes, or text to stdout.

What htmlq Does

  • Selects HTML elements using standard CSS selector syntax
  • Extracts specific attributes with the -a flag (e.g., -a href for links)
  • Outputs plain text content with -t, stripping all HTML tags
  • Pipes cleanly with curl, wget, or any command that outputs HTML
  • Pretty-prints matched HTML with proper indentation using -p

Architecture Overview

htmlq is a single-binary CLI tool written in Rust. It uses the html5ever parser (the same HTML parser used by Firefox's Servo engine) for spec-compliant DOM construction, and the kuchikiki library for CSS selector matching. The tool reads the entire HTML document into memory, applies the selector, and streams matching nodes to stdout.

Self-Hosting & Configuration

  • Install with cargo install htmlq from crates.io
  • Available via Homebrew on macOS: brew install htmlq
  • Available on most Linux distributions via package managers
  • No configuration file needed; all options are command-line flags
  • Works with piped input only; does not fetch URLs on its own

Key Features

  • CSS3 selector syntax for precise element targeting
  • Attribute extraction mode for scraping links, images, or metadata
  • Text-only output mode for clean data extraction
  • Pretty-print mode for readable HTML output
  • Small static binary with no runtime dependencies

Comparison with Similar Tools

  • pup — similar HTML selector tool written in Go, supports pseudo-classes
  • xidel — more powerful with XPath and XQuery, but heavier
  • hq — Python-based HTML/JSON query tool with jq-like syntax
  • Beautiful Soup — Python library for HTML parsing, not a CLI tool

FAQ

Q: Can htmlq fetch URLs directly? A: No. Pipe HTML into it from curl, wget, or a file: curl -s URL | htmlq 'selector'.

Q: Does it support XPath? A: No, only CSS selectors. Use xidel if you need XPath support.

Q: Can I chain multiple selectors? A: Yes. Standard CSS combinator syntax works: htmlq 'div.content > p'.

Q: What license does htmlq use? A: MIT license.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产