Using Hpricot::Elements

Part of AnHpricotShowcase.

Searching a Set of Elements

at( expression, &block )

Find a single element which match the CSS or XPath expression. If a block is given, the matching element is passed to the block and the original set of Elements is returned.

 #!ruby
 doc.search("div.entryPermalink").at("a")

The above returns the first link found among all the div.entryPermalink elements.

search( expression, &block )

Finds all elements which match the CSS or XPath expression. If a block is given, the matching elements are passed to the block, in turn, and the original set of Elements is returned.

 #!ruby
 doc.search("div.entryPermalink").search("a") do |link|
   pp link
 end.search("span") do |span|
   pp span
 end

The above searches first for all links in div.entryPermalink elements, then for all span tags in div.entryPermalink elements.

Altering All Elements in a Set

append( html_string )

Add HTML from html_string within each element, to the end of each element's content.

 #!ruby
 doc.search("div.entryPermalink").append(" » " + Time.now)

The above adds a timestamp just inside the end of each div.entryPermalink element.

prepend( html_string )

Add HTML from html_string with each element, to the beginning of each element's content.

 #!ruby
 doc.search("a[@href]").prepend("<h1>Link</h1>")

The above adds a heading just inside the beginning of each link.

wrap( html_string )

Wraps each element in the set inside the element created by html_string. If more than one element is found in the html_string, Hpricot locates the deepest spot inside the first element.

 #!ruby
 doc.search("a[@href]").wrap(%{<div class="link"><div class="link_inner"></div></div>})

This code wraps every link on the page inside a div.link and a div.link_inner nest.


Return to AnHpricotShowcase