-
Notifications
You must be signed in to change notification settings - Fork 15
Hpricot Elements
(Part of An Hpricot Showcase.)
Find a single element which matches 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.
doc.search("div.entryPermalink").at("a")
The above returns the first link found among all the div.entryPermalink
elements.
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.
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.
Add HTML from html_string
within each element, to the end of each element’s content.
doc.search("div.entryPermalink").append(" » " + Time.now)
The above adds a timestamp just inside the end of each div.entryPermalink
element.
Add HTML from html_string
with each element, to the beginning of each element’s content.
doc.search("a[@href]").prepend("<h1>Link</h1>")
The above adds a heading just inside the beginning of each link.
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.
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 An Hpricot Showcase.