# Hpricot 0.6 -- the swift, delightful HTML parser

**URL:** <https://www.ruby-forum.com/t/hpricot-0-6-the-swift-delightful-html-parser/106982>\
**Category:** Ruby\
**Created:** [June 16, 2007, 2:02am UTC](https://www.ruby-forum.com/t/hpricot-0-6-the-swift-delightful-html-parser/106982 "2007-06-16T02:02:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_why](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a3d4f5/32.png) [@S\_why](https://www.ruby-forum.com/u/S_why)\
**Post date:** [June 16, 2007, 2:02am UTC](https://www.ruby-forum.com/t/hpricot-0-6-the-swift-delightful-html-parser/106982/1 "2007-06-16T02:02:31Z")

</div>

Hpricot 0.6 is up on Rubyforge. Or get it from the development gem  
server:

gem install hpricot --source [http://code.whytheluckystiff.net](http://code.whytheluckystiff.net)

Hpricot is a flexible HTML parser written in C. Nestled in a nice  
Ruby wrapper. But Hpricot takes a lot of extra steps to help you  
out.

Hpricot is great for both scraping web sites and altering HTML  
safely. With plenty of options for either cleaning up HTML or  
leaving unmodified areas untouched.

HPRICOT’s HOME PAGE and WIKI  
[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)

HPRICOT’s DOCUMENTATION  
[http://code.whytheluckystiff.net/doc/hpricot/](http://code.whytheluckystiff.net/doc/hpricot/)

= EXAMPLES =

Loading an HTML page:

require ‘open-uri’  
require ‘hpricot’

doc = Hpricot(open(“[http://ruby-lang.org/](http://ruby-lang.org/)”))

Fixing HTML into XHTML:

doc = Hpricot(open(“[http://ruby-lang.org/](http://ruby-lang.org/)”), :fixup\_tags =\> true)

Placing a number next to each link on a page, preserving the  
original HTML as much as possible:

doc = Hpricot(open(“[http://ruby-lang.org/](http://ruby-lang.org/)”))  
num = 0  
(doc/“a”).append do  
strong " [#{num += 1}]"  
end  
puts doc.to\_original\_html

(Notice how you can use a simple Ruby syntax for adding HTML tags  
inside the block attached to the `append` method!)

= CHANGELOG =

- Hpricot for JRuby – nice work Ola B.!
- Inline Markaby for Hpricot documents.
- XML tags and attributes are no longer downcased like HTML is.
- new syntax for grabbing everything between two elements using a  
Range in the search method: (doc/(“font”…“font/br”)) or in nodes\_at  
like so: (doc/“font”).nodes\_at(“\*”…“br”). Only works with either a pair  
of siblings or a set of a parent and a sibling.
- Ignore self-closing endings on tags (such as form) which are  
containers. Treat them like open parent tags. Reported by Jonathan  
Nichols on the hpricot list.
- Escaping of attributes, yanked from Jim W. and Sam Ruby’s work  
in Builder.
- Element#raw\_attributes gives unescaped data. Element#attributes  
gives escaped.
- Added: Elements#attr, Elements#remove\_attr, Elements#remove\_class.
- Added: Traverse#preceding, Traverse#following, Traverse#previous,  
Traverse#next.

Okay, good enough,

\_why
