Search for the first instance of an element only : hpricot HTML Parsing « Network « Ruby






Search for the first instance of an element only



require 'rubygems'
require 'hpricot'
require 'open-uri'

doc = Hpricot(open('http://www.rubyinside.com/test.html'))
puts doc.search("h1").first.inner_html


list = doc.at("ul")
list.search("li").each do |item|
  puts item.inner_html
end

 








Related examples in the same category

1.require 'rubygems'
2.Hpricot can work directly with open-uri to load HTML from remote files
3.Using a combination of search methods, search for the list within the HTML and then extract each item
4.Using CSS classes to find certain elements