123456789_123456789_123456789_123456789_123456789_

Module: Nokogiri

Relationships & Source Files
Namespace Children
Modules:
Classes:
Exceptions:
Defined in: lib/nokogiri.rb,
ext/nokogiri/nokogiri.c,
lib/nokogiri/class_resolver.rb,
lib/nokogiri/css.rb,
lib/nokogiri/encoding_handler.rb,
lib/nokogiri/gumbo.rb,
lib/nokogiri/html.rb,
lib/nokogiri/html4.rb,
lib/nokogiri/html5.rb,
lib/nokogiri/syntax_error.rb,
lib/nokogiri/xml.rb,
lib/nokogiri/xslt.rb,
lib/nokogiri/css/node.rb,
lib/nokogiri/css/parser.rb,
lib/nokogiri/css/parser.rb,
lib/nokogiri/css/parser_extras.rb,
lib/nokogiri/css/selector_cache.rb,
lib/nokogiri/css/syntax_error.rb,
lib/nokogiri/css/tokenizer.rb,
lib/nokogiri/css/xpath_visitor.rb,
lib/nokogiri/decorators/slop.rb,
lib/nokogiri/html4/builder.rb,
lib/nokogiri/html4/document.rb,
lib/nokogiri/html4/document_fragment.rb,
lib/nokogiri/html4/element_description.rb,
lib/nokogiri/html4/element_description_defaults.rb,
lib/nokogiri/html4/encoding_reader.rb,
lib/nokogiri/html4/entity_lookup.rb,
lib/nokogiri/html4/sax/parser.rb,
lib/nokogiri/html4/sax/parser_context.rb,
lib/nokogiri/html4/sax/push_parser.rb,
lib/nokogiri/html5/builder.rb,
lib/nokogiri/html5/document.rb,
lib/nokogiri/html5/document_fragment.rb,
lib/nokogiri/html5/node.rb,
lib/nokogiri/jruby/nokogiri_jars.rb,
lib/nokogiri/version/constant.rb,
lib/nokogiri/version/info.rb,
lib/nokogiri/xml/attr.rb,
lib/nokogiri/xml/attribute_decl.rb,
lib/nokogiri/xml/builder.rb,
lib/nokogiri/xml/cdata.rb,
lib/nokogiri/xml/character_data.rb,
lib/nokogiri/xml/document.rb,
lib/nokogiri/xml/document_fragment.rb,
lib/nokogiri/xml/dtd.rb,
lib/nokogiri/xml/element_content.rb,
lib/nokogiri/xml/element_decl.rb,
lib/nokogiri/xml/entity_decl.rb,
lib/nokogiri/xml/entity_reference.rb,
lib/nokogiri/xml/namespace.rb,
lib/nokogiri/xml/node.rb,
lib/nokogiri/xml/node_set.rb,
lib/nokogiri/xml/notation.rb,
lib/nokogiri/xml/parse_options.rb,
lib/nokogiri/xml/processing_instruction.rb,
lib/nokogiri/xml/reader.rb,
lib/nokogiri/xml/relax_ng.rb,
lib/nokogiri/xml/sax.rb,
lib/nokogiri/xml/schema.rb,
lib/nokogiri/xml/searchable.rb,
lib/nokogiri/xml/syntax_error.rb,
lib/nokogiri/xml/text.rb,
lib/nokogiri/xml/xpath.rb,
lib/nokogiri/xml/xpath_context.rb,
lib/nokogiri/xml/node/save_options.rb,
lib/nokogiri/xml/pp/character_data.rb,
lib/nokogiri/xml/pp/node.rb,
lib/nokogiri/xml/sax/document.rb,
lib/nokogiri/xml/sax/parser.rb,
lib/nokogiri/xml/sax/parser_context.rb,
lib/nokogiri/xml/sax/push_parser.rb,
lib/nokogiri/xml/xpath/syntax_error.rb,
lib/nokogiri/xslt/stylesheet.rb

Overview

Nokogiri parses and searches XML/HTML very quickly, and also has correctly implemented CSS3 selector support as well as XPath 1.0 support.

Parsing a document returns either a ::Nokogiri::XML::Document, or a ::Nokogiri::HTML4::Document depending on the kind of document you parse.

Here is an example:

require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML4::Document for the page we’re interested in...

doc = Nokogiri::HTML4(URI.open('http://www.google.com/search?q=tenderlove'))

# Do funky things with it using Nokogiri::XML::Node methods...

####
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
  puts link.content
end

See also:

Constant Summary

Class Attribute Summary

Class Method Summary

Class Attribute Details

.jruby?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/nokogiri/version/info.rb', line 206

def self.jruby?
  VersionInfo.instance.jruby?
end

.uses_gumbo?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/nokogiri/version/info.rb', line 201

def self.uses_gumbo?
  uses_libxml? # TODO: replace with Gumbo functionality
end

Class Method Details

HTML(input, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block) → Nokogiri::HTML4::Document)

Parse HTML. Convenience method for HTML4::Document.parse

[ GitHub ]

  
# File 'lib/nokogiri/html.rb', line 10

RDoc directive :singleton-method: HTML

HTML4

Parse HTML. Convenience method for HTML4::Document.parse

[ GitHub ]

  
# File 'lib/nokogiri/html4.rb', line 7

def HTML4(...)
  Nokogiri::HTML4::Document.parse(...)
end

HTML5(input, url = nil, encoding = nil, **options, &block)

Since v1.12.0

::Nokogiri::HTML5 functionality is not available when running JRuby.

Parse an ::Nokogiri::HTML5 document. Convenience method for HTML5::Document.parse

[ GitHub ]

  
# File 'lib/nokogiri/html5.rb', line 31

def self.HTML5(input, url = nil, encoding = nil, **options, &block)
  Nokogiri::HTML5::Document.parse(input, url, encoding, **options, &block)
end

.install_default_aliases

This method is for internal use only.
[ GitHub ]

  
# File 'lib/nokogiri.rb', line 96

def install_default_aliases
  warn("Nokogiri.install_default_aliases is deprecated. Please call Nokogiri::EncodingHandler.install_default_aliases instead. This will become an error in Nokogiri v1.17.0.", uplevel: 1, category: :deprecated) # deprecated in v1.14.0, remove in v1.17.0
  Nokogiri::EncodingHandler.install_default_aliases
end

.libxml2_patches

This method is for internal use only.
[ GitHub ]

  
# File 'lib/nokogiri/version/info.rb', line 211

def self.libxml2_patches
  if VersionInfo.instance.libxml2_using_packaged?
    Nokogiri::VERSION_INFO["libxml"]["patches"]
  else
    []
  end
end

.make(input = nil, opts = {}, &blk)

[ GitHub ]

  
# File 'lib/nokogiri.rb', line 68

def make(input = nil, opts = {}, &blk)
  if input
    Nokogiri::HTML4.fragment(input).children.first
  else
    Nokogiri(&blk)
  end
end

.parse(string, url = nil, encoding = nil, options = nil)

Parse an ::Nokogiri::HTML or ::Nokogiri::XML document. string contains the document.

[ GitHub ]

  
# File 'lib/nokogiri.rb', line 42

def parse(string, url = nil, encoding = nil, options = nil)
  if string.respond_to?(:read) ||
      /^\s*<(?:!DOCTYPE\s+)?html[\s>]/i.match?(string[0, 512])
    # Expect an HTML indicator to appear within the first 512
    # characters of a document. (<?xml ?> + <?xml-stylesheet ?>
    # shouldn't be that long)
    Nokogiri.HTML4(
      string,
      url,
      encoding,
      options || XML::ParseOptions::DEFAULT_HTML,
    )
  else
    Nokogiri.XML(
      string,
      url,
      encoding,
      options || XML::ParseOptions::DEFAULT_XML,
    )
  end.tap do |doc|
    yield doc if block_given?
  end
end

Slop(*args, &block)

Parse a document and add the Slop decorator. The Slop decorator implements method_missing such that methods may be used instead of ::Nokogiri::CSS or XPath. For example:

doc = Nokogiri::Slop(<<-eohtml)
  <html>
    <body>
      <p>first</p>
      <p>second</p>
    </body>
  </html>
eohtml
assert_equal('second', doc.html.body.p[1].text)
[ GitHub ]

  
# File 'lib/nokogiri.rb', line 91

def Slop(*args, &block)
  Nokogiri(*args, &block).slop!
end

.uses_libxml?(requirement = nil) ⇒ Boolean

This method is for internal use only.
[ GitHub ]

  
# File 'lib/nokogiri/version/info.rb', line 193

def self.uses_libxml?(requirement = nil)
  return false unless VersionInfo.instance.libxml2?
  return true unless requirement

  Gem::Requirement.new(requirement).satisfied_by?(VersionInfo.instance.loaded_libxml_version)
end

XML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block)

Parse XML. Convenience method for XML::Document.parse

[ GitHub ]

  
# File 'lib/nokogiri/xml.rb', line 7

def XML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block)
  Nokogiri::XML::Document.parse(thing, url, encoding, options, &block)
end

XSLT(stylesheet, modules = {})

Create a ::Nokogiri::XSLT::Stylesheet with stylesheet.

Example:

xslt = Nokogiri::XSLT(File.read(ARGV[0]))
[ GitHub ]

  
# File 'lib/nokogiri/xslt.rb', line 13

def XSLT(stylesheet, modules = {})
  XSLT.parse(stylesheet, modules)
end