Hpricot - HTML parser for Ruby

May 27th, 2007

Hpricot is a very interesting lib to parse HTML. I’ll post here the example, just to check on the beauty of it from time to time…


require 'hpricot'
require 'open-uri'
# load the RedHanded home page
doc = Hpricot(open("http://redhanded.hobix.com/index.html"))
# change the CSS class on links
(doc/"span.entryPermalink").set("class", "newLinks")
# remove the sidebar
(doc/"#sidebar").remove
# print the altered HTML
puts doc

The lib is evolving, being added support for more XPath functions, etc.

RedHanded’s blog from the same author, has another cool example. why rocks!


Leave a Reply