Skip to content
tenderlove edited this page Sep 12, 2010 · 22 revisions

Welcome to the nokogiri wiki!

require 'nokogiri'

doc = Nokogiri::HTML.parse(<<-eohtml)
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>This is an awesome document</h1>
    <p>
      I am a paragraph
      <a>I am a link</a>
    </p>
  </body>
</html>
eohtml

####
# Search for nodes by css
doc.find_by_css('p > a').each do |a_tag|
  puts a_tag.content
end

####
# Search for nodes by xpath
doc.find_by_xpath('//p/a').each do |a_tag|
  puts a_tag.content
end

####
# Or mix and match.
doc.search('//p/a', 'p > a').each do |a_tag|
  puts a_tag.content
end