W3cubDocs

/Nokogiri

module Nokogiri

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::HTML::Document depending on the kind of document you parse.

Here is an example:

require 'nokogiri'
require 'open-uri'

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

doc = Nokogiri::HTML(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 Nokogiri::XML::Searchable#css for more information about CSS searching. See Nokogiri::XML::Searchable#xpath for more information about XPath searching.

Constants

VERSION

The version of Nokogiri you are using

VERSION_INFO

More complete version information about libxml

Public Class Methods

HTML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block) Show source
# File lib/nokogiri/html.rb, line 14
def HTML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
  Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
end

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

Slop(*args, &block) Show source
# File lib/nokogiri.rb, line 113
def Slop(*args, &block)
  Nokogiri(*args, &block).slop!
end

Parse a document and add the Slop decorator. The Slop decorator implements method_missing such that methods may be used instead of 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)
XML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block) Show source
# File lib/nokogiri/xml.rb, line 34
def XML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block
  Nokogiri::XML::Document.parse(thing, url, encoding, options, &block)
end

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

XSLT(stylesheet, modules = {}) Show source
# File lib/nokogiri/xslt.rb, line 12
def XSLT stylesheet, modules = {}
  XSLT.parse(stylesheet, modules)
end

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

Example:

xslt = Nokogiri::XSLT(File.read(ARGV[0]))
install_default_aliases() Show source
# File lib/nokogiri.rb, line 117
def install_default_aliases
  # Make sure to support some popular encoding aliases not known by
  # all iconv implementations.
  {
    'Windows-31J' => 'CP932',       # Windows-31J is the IANA registered name of CP932.
  }.each { |alias_name, name|
    EncodingHandler.alias(name, alias_name) if EncodingHandler[alias_name].nil?
  }
end
make(input = nil, opts = {}) Show source
# File lib/nokogiri.rb, line 90
def make input = nil, opts = {}, &blk
  if input
    Nokogiri::HTML.fragment(input).children.first
  else
    Nokogiri(&blk)
  end
end

Create a new Nokogiri::XML::DocumentFragment

parse(string, url = nil, encoding = nil, options = nil) { |doc| ... } Show source
# File lib/nokogiri.rb, line 72
def parse string, url = nil, encoding = nil, options = nil
  if string.respond_to?(:read) ||
      /^\s*<(?:!DOCTYPE\s+)?html[\s>]/i === 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.HTML(string, url, encoding,
      options || XML::ParseOptions::DEFAULT_HTML)
  else
    Nokogiri.XML(string, url, encoding,
      options || XML::ParseOptions::DEFAULT_XML)
  end.tap { |doc|
    yield doc if block_given?
  }
end

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

© 2008–2018 Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo,
Patrick Mahoney, Yoko Harada, Akinori MUSHA, John Shahid, Lars Kanis
Licensed under the MIT License.