Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsSep 12, 2026·3 min de lectura

Nokogiri — Fast HTML and XML Parser for Ruby

Nokogiri is the most widely used Ruby gem for parsing and searching HTML and XML documents. Built on libxml2 and libgumbo, it provides CSS selector and XPath query interfaces with robust encoding support.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Nokogiri XML HTML Parser
Comando de instalación directa
npx -y tokrepo@latest install 04eeef23-ae4b-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

Introduction

Nokogiri wraps the battle-tested libxml2 and libgumbo C libraries to give Ruby developers fast, standards-compliant parsing of HTML and XML documents. It handles malformed markup gracefully and provides both CSS selector and XPath interfaces for querying document trees.

What Nokogiri Does

  • Parses HTML4, HTML5, XML, and XSLT documents into traversable node trees
  • Supports both CSS selector and XPath query syntax for searching nodes
  • Handles broken and malformed HTML gracefully with automatic error recovery
  • Provides a builder DSL for generating XML and HTML programmatically
  • Detects and converts character encodings transparently

Architecture Overview

Nokogiri wraps two C libraries: libxml2 for XML and HTML4 parsing, and libgumbo for HTML5 parsing. Ruby objects mirror the underlying C document tree through a thin binding layer. When you call css() or xpath(), the query is dispatched to the C library's native search implementation, which is significantly faster than a pure-Ruby approach. Precompiled native gems for major platforms eliminate the need to compile C extensions on most systems.

Self-Hosting & Configuration

  • Install via gem install nokogiri; precompiled binaries cover Linux, macOS, and Windows
  • Use Nokogiri::HTML() for HTML4 or Nokogiri::HTML5() for spec-compliant HTML5 parsing
  • Pass encoding options explicitly when dealing with non-UTF-8 documents
  • Configure parse options like NOBLANKS, NOERROR, or RECOVER for strict or lenient parsing
  • Use Bundler's platform constraints to ensure the correct native gem is selected in CI

Key Features

  • Native C extensions backed by libxml2 and libgumbo for high throughput
  • Dual query API with CSS selectors and full XPath 1.0 support
  • HTML5 parsing that follows the WHATWG specification
  • SAX and Reader streaming parsers for processing large documents without loading them fully into memory
  • Precompiled native gems that install without requiring a C toolchain

Comparison with Similar Tools

  • Oga — Pure-Ruby XML/HTML parser; no native dependencies but significantly slower on large documents
  • REXML — Ruby standard library XML parser; always available but limited features and slow
  • Ox — Fast Ruby XML parser optimized for speed; lacks CSS selector support
  • Hpricot — Legacy Ruby HTML parser; unmaintained, superseded by Nokogiri
  • Beautiful Soup (Python) — Equivalent role in Python; Nokogiri is the Ruby counterpart

FAQ

Q: Do I need to install libxml2 separately? A: Usually no. Precompiled native gems bundle the required C libraries. Building from source requires libxml2 and libxslt development headers.

Q: How do I parse malformed HTML without errors? A: Use Nokogiri::HTML() which enables error recovery by default. Pass Nokogiri::XML::ParseOptions::RECOVER for XML documents.

Q: Can Nokogiri handle very large files? A: Yes. Use the SAX parser or Reader interface to process documents as a stream without loading the entire tree into memory.

Q: Is Nokogiri thread-safe? A: Individual document objects are not thread-safe. Parse and query documents within a single thread, or protect shared documents with a mutex.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados