# Need help parsing HTML with Hpricot

**URL:** <https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269>\
**Category:** Ruby\
**Created:** [October 25, 2007, 9:00am UTC](https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269 "2007-10-25T09:00:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 25, 2007, 9:00am UTC](https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269/1 "2007-10-25T09:00:18Z")

</div>

I’m having trouble understanding Hpricot (thanks to an abominable lack  
of documentation). I’m trying to parse HTML of the following nature:

This is one line of text  
  
This is another line of text  
  
It keeps going on like this  
  
  
  
Until a new paragraph is started  
  
Otherwise, it’s just more of the same

```
I know, it looks simple but, frankly, I have no clue how to parse 

```

this  
with Hpricot. Particularly, I don’t know how to single out the lines of  
text in between the “br” tags. This is important 'cause I need to know  
where the line breaks are in the text, as well as the new paragraphs.  
Does anyone know how to do this with Hpricot?  
Thank you…

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 25, 2007, 9:37am UTC](https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269/2 "2007-10-25T09:37:45Z")

</div>

You can try each\_child.

I will use each\_child\_with\_index to show you what I mean:

Put your raw HTML text into @text

@parsed\_html = Hpricot(@text)  
@parsed\_html.each\_child\_with\_index do |c,i|  
puts “Line #{i}: #{c.to\_s.strip}”  
end

Produces:

Line 0: This is one line of text  
Line 1:   
  
Line 2: This is another line of text  
Line 3:   
  
Line 4: It keeps going on like this  
Line 5:   
  
Line 6:  
Line 7:   
  
Line 8: Until a new paragraph is started  
Line 9:   
  
Line 10: Otherwise, it’s just more of the same  
Line 11:   
  
Line 12:

Hope that helps.

Mikel

On 10/25/07, Just Another Victim of the Ambient M.

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 25, 2007, 9:48am UTC](https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269/3 "2007-10-25T09:48:18Z")

</div>

2007/10/25, Just Another Victim of the Ambient M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> I'm having trouble understanding Hpricot (thanks to an abominable lack
> 
> ```
> 
> of documentation). I’m trying to parse HTML of the following nature:

Try [http://code.whytheluckystiff.net/hpricot/wiki/AnHpricotShowcase](http://code.whytheluckystiff.net/hpricot/wiki/AnHpricotShowcase)  
for examples and some better documentation. It helped me a lot to  
solve my problems.

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 25, 2007, 9:50am UTC](https://www.ruby-forum.com/t/need-help-parsing-html-with-hpricot/118269/4 "2007-10-25T09:50:04Z")

</div>

Of course… you could also do:

require ‘rubygems’  
require ‘hpricot’

text =\<\<HERE  
This is one line of text  
  
This is another line of text  
  
It keeps going on like this  
  
  
  
Until a new paragraph is started  
  
Otherwise, it’s just more of the same  
  
HERE

class String  
def not\_needed?  
self.strip == “  
” ? true : false  
end  
end

@parsed\_html = Hpricot(text)  
@paragraphs = Array.new  
@parsed\_html.each\_child\_with\_index do |c,i|  
line = c.to\_s.strip  
if line == “”  
puts “

#{@paragraphs}

”  
@paragraphs.clear  
else  
@paragraphs \<\< "#{line} " unless line.not\_needed?  
end  
end

Which produces:

This is one line of text This is another line of text It keeps going on like this

Until a new paragraph is started Otherwise, it's just more of the same

Now… don’t pick on my favorite HTML parser again! 😃 Just ask nicely  
🙂

Mikel
