Skip to main content

A command-line HTML processor inspired by jq

Project description

hq - HTML Query Tool

A command-line HTML processor inspired by jq. Parse and extract data from HTML with jq-like syntax.

Features

  • jq-like syntax: Familiar query syntax for HTML processing
  • CSS selectors: Support for class (.class) and ID (#id) selectors
  • Attribute extraction: Get attributes or filter by attribute values
  • Syntax highlighting: Colored output for better readability
  • Multiple output formats: HTML, text, JSON, and raw formats
  • Pipe operations: Chain multiple operations together
  • Pretty formatting: Nicely formatted HTML output

Installation

pip install hq-html

Usage

# Basic: parse HTML from stdin
curl http://example.com | hq

# Extract specific elements
curl http://example.com | hq 'title'
curl http://example.com | hq 'h1 | text'

Examples

Basic Selectors

# Get title elements
curl http://example.com | hq 'title'

# Get all paragraphs
curl http://example.com | hq 'p'

# Get elements by class
curl http://example.com | hq '.content'

# Get elements by ID
curl http://example.com | hq '#main'

Compound Selectors

# Get paragraphs inside content class
curl http://example.com | hq '.content p'

# Get links inside navigation
curl http://example.com | hq '#nav a'

# Complex nested selection
curl http://example.com | hq '#main .content p'

Attribute Selectors

# Get href attributes from links
curl http://example.com | hq 'a @href'

# Get input elements by type
curl http://example.com | hq 'input[type=text]'

# Get elements with specific attributes
curl http://example.com | hq 'img[alt]'

# Get image sources
curl http://example.com | hq 'img @src'

Text Extraction

# Get text content only
curl http://example.com | hq 'title | text'

# Get text from multiple elements
curl http://example.com | hq 'p | text'

# Get text from headings
curl http://example.com | hq 'h1 | text'

Output Formats

# HTML output (default)
curl http://example.com | hq 'title'

# Text output
curl http://example.com | hq --output text 'p'

# JSON output
curl http://example.com | hq --output json 'title'

# Raw output (no formatting)
curl http://example.com | hq --raw 'title | text'

# Compact output
curl http://example.com | hq --compact 'title'

Chaining Operations

# Chain operations with pipes
curl http://example.com | hq '.content | p | text'

# Get first element
curl http://example.com | hq 'li | [0]'

# Get specific attribute
curl http://example.com | hq 'a | @href'

# Count elements
curl http://example.com | hq 'div | length'

Syntax Reference

  • Tag selectors: div, p, a, img, etc.
  • Class selectors: .className
  • ID selectors: #idName
  • Attribute selectors: [attr=value], [attr]
  • Compound selectors: .class tag, #id .class
  • Attribute extraction: @href, @src, @alt
  • Text extraction: | text
  • HTML extraction: | html
  • Array indexing: | [0], | [1]
  • Length: | length
  • Piping: selector | operation | filter

Options

  • --output, -o: Output format (html, text, json)
  • --raw, -r: Raw output without formatting
  • --compact, -c: Compact JSON output
  • --color/--no-color: Enable/disable syntax highlighting
  • --help: Show help message with examples

Real-world Examples

# Extract all article titles from Hacker News
curl -s https://news.ycombinator.com | hq '.titleline a | text'

# Get all external links from a page
curl -s http://example.com | hq 'a @href'

# Extract meta description
curl -s http://example.com | hq 'meta[name=description] @content'

# Get all image URLs
curl -s http://example.com | hq 'img @src'

# Extract form input names
curl -s http://example.com | hq 'input @name'

# Get navigation menu items
curl -s http://example.com | hq 'nav a | text'

Why hq?

While jq is excellent for JSON, there wasn't a similar tool for HTML. hq fills this gap by providing:

  • Intuitive syntax: If you know CSS selectors and jq, you already know hq
  • HTML-first design: Built specifically for HTML parsing and extraction
  • Beautiful output: Syntax-highlighted, properly formatted results
  • Flexible querying: From simple tag extraction to complex nested selections

Development

Setting up the development environment

# Clone the repository
git clone https://github.com/Julestblt/hq.git
cd hq

# Install in development mode
pip install -e .[dev]

# Run tests
pytest

# Run quality checks
black --check .
flake8 .
mypy hq/
isort --check-only .

Versioning and Releases

This project uses automated versioning and publishing:

  • Automatic releases: Every push to main automatically bumps the patch version, creates a git tag, and publishes to PyPI
  • Manual releases: Use the "Manual Version Bump" GitHub Action to bump major/minor versions
  • Version management: Uses bump2version to keep version numbers in sync across files

The version is automatically updated in:

  • pyproject.toml
  • hq/__init__.py
  • .bumpversion.cfg

CI/CD Pipeline

  • Tests: Run on Python 3.9, 3.10, 3.11, and 3.12
  • Quality checks: Black, Flake8, MyPy, and isort
  • Automated publishing: On successful tests, packages are built and published to PyPI

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hq_html-0.1.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hq_html-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file hq_html-0.1.1.tar.gz.

File metadata

  • Download URL: hq_html-0.1.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hq_html-0.1.1.tar.gz
Algorithm Hash digest
SHA256 02a631575bca960a99d57442147d9db30337554ba137a07e4efa49d83567b5c7
MD5 cf2293e554c8a4c8aea57b36e940bcc9
BLAKE2b-256 9014600f409348fc1baf65d42111ddcbd7769a9587a7b756a1b836a27f94bc39

See more details on using hashes here.

File details

Details for the file hq_html-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hq_html-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for hq_html-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 710f65598ef2e56b19263d45b229f24bebfd3977cea12ecc2f8784b2c41cb27c
MD5 0e3da7756631ab84477065085c483077
BLAKE2b-256 baa922aeb8f97b2c30194fd140cb8727915b0bd5935740e913ee62c27d1ce904

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page