# Hpricot parsing

**URL:** <https://rubytalk.org/t/hpricot-parsing/52943>\
**Category:** ruby-talk\
**Created:** [19 April 2009 16:12 UTC](https://rubytalk.org/t/hpricot-parsing/52943 "2009-04-19T16:12:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Farber](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@Marc\_Farber](https://rubytalk.org/u/Marc_Farber)\
**Post date:** [19 April 2009 16:12 UTC](https://rubytalk.org/t/hpricot-parsing/52943/1 "2009-04-19T16:12:27Z")

</div>

Ruby newbie here

Have successfully used hpricot to scrape correct \<div\> from desired page  
[http://www.montgomeryadvertiser.com/section/obits](http://www.montgomeryadvertiser.com/section/obits) using

doc = Hpricot(uri above)  
...  
@grab1 = doc.search("//div[@class='article-bodytext']")

target data is in following logical form

\<div\>  
\<h3\>name of funeral home\</h3\>  
\<p\>deceased1\</p\>  
\<div\>advertising crap\</div\>  
\<h3\>funeral home 2\</h3\>  
\<p\>deceased 2\</p\>  
\<p\>deceased 3\</p\>  
\</div\>

I'm struggling to iterate thru this div, plucking a array or hash where  
I can feed a database with each record being a funeral home and person.  
I was thinking I could go thru each of the @grab1 elements and process  
according to tag type and establish the "record" logic thru simple  
knowing that a new record starts with each new h3 tag.

Any help for a newbie with first Ruby script?

Thx

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [19 April 2009 23:20 UTC](https://rubytalk.org/t/hpricot-parsing/52943/2 "2009-04-19T23:20:43Z")

</div>

Marc Farber wrote:

> Ruby newbie here
> 
> Have successfully used hpricot to scrape correct \<div\> from desired page  
> [http://www.montgomeryadvertiser.com/section/obits](http://www.montgomeryadvertiser.com/section/obits) using
> 
> doc = Hpricot(uri above)  
> ...  
> @grab1 = doc.search("//div[@class='article-bodytext']")
> 
> target data is in following logical form
> 
> \<div\>  
> \<h3\>name of funeral home\</h3\>  
> \<p\>deceased1\</p\>  
> \<div\>advertising crap\</div\>  
> \<h3\>funeral home 2\</h3\>  
> \<p\>deceased 2\</p\>  
> \<p\>deceased 3\</p\>  
> \</div\>
> 
> I'm struggling to iterate thru this div..  
> I [want to insert a record into a table with each] record being a funeral home and person.  
> I was thinking I could go thru each of the @grab1 elements and process  
> according to tag type:

These methods seem like the ones you need:

elm.next\_sibling (skips the newlines in the html)  
elm.name

How about this:

require "rubygems"  
require 'hpricot'

str =\<\<ENDOFSTRING  
\<div\>  
&nbsp;&nbsp;\<h3\>name of funeral home\</h3\>  
&nbsp;&nbsp;\<p\>deceased1\</p\>  
&nbsp;&nbsp;\<div\>advertising crap\</div\>  
&nbsp;&nbsp;\<h3\>funeral home 2\</h3\>  
&nbsp;&nbsp;\<p\>deceased 2\</p\>  
&nbsp;&nbsp;\<p\>deceased 3\</p\>  
\</div\>  
ENDOFSTRING

doc = Hpricot(str)  
h3\_tags = doc.search("h3")

h3\_tags.each do |h3|  
&nbsp;&nbsp;elm = h3

&nbsp;&nbsp;while elm = elm.next\_sibling  
&nbsp;&nbsp;&nbsp;&nbsp;break if elm.name != 'p'

&nbsp;&nbsp;&nbsp;&nbsp;puts h3.inner\_text  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\t #{elm.inner\_text}"  
&nbsp;&nbsp;end

end

--output:--  
name of funeral home  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deceased1  
funeral home 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deceased 2  
funeral home 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deceased 3

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Wang\_Jian](https://avatars.discourse-cdn.com/v4/letter/w/5f8ce5/32.png) [@Wang\_Jian](https://rubytalk.org/u/Wang_Jian)\
**Post date:** [20 April 2009 02:04 UTC](https://rubytalk.org/t/hpricot-parsing/52943/3 "2009-04-20T02:04:22Z")

</div>

Makes me wonder if ReXML, Hpricot or Nokogiri has a to\_hash method...not yet  
found.  
I'd also be glad to know.

> **···**
>
> 2009/4/20 Marc Farber \<mrcfab3@gmail.com\>
> 
> > Ruby newbie here
> > 
> > Have successfully used hpricot to scrape correct \<div\> from desired page  
> > [http://www.montgomeryadvertiser.com/section/obits](http://www.montgomeryadvertiser.com/section/obits) using
> > 
> > doc = Hpricot(uri above)  
> > ...  
> > @grab1 = doc.search("//div[@class='article-bodytext']")
> > 
> > target data is in following logical form
> > 
> > \<div\>  
> > \<h3\>name of funeral home\</h3\>  
> > \<p\>deceased1\</p\>  
> > \<div\>advertising crap\</div\>  
> > \<h3\>funeral home 2\</h3\>  
> > \<p\>deceased 2\</p\>  
> > \<p\>deceased 3\</p\>  
> > \</div\>
> > 
> > I'm struggling to iterate thru this div, plucking a array or hash where  
> > I can feed a database with each record being a funeral home and person.  
> > I was thinking I could go thru each of the @grab1 elements and process  
> > according to tag type and establish the "record" logic thru simple  
> > knowing that a new record starts with each new h3 tag.
> > 
> > Any help for a newbie with first Ruby script?
> > 
> > Thx  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [19 April 2009 23:40 UTC](https://rubytalk.org/t/hpricot-parsing/52943/4 "2009-04-19T23:40:35Z")

</div>

7stud -- wrote:

> h3\_tags.each do |h3|  
> &nbsp;&nbsp;elm = h3
> 
> &nbsp;&nbsp;while elm = elm.next\_sibling  
> &nbsp;&nbsp;&nbsp;&nbsp;break if elm.name != 'p'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;puts h3.inner\_text  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "\t #{elm.inner\_text}"  
> &nbsp;&nbsp;end
> 
> end

To avoid having to lookup the inner\_text of the funeral home for each  
deceased person at that funeral home, this would be more efficient:

h3\_tags.each do |elm|  
&nbsp;&nbsp;funeral\_home = elm.inner\_text

&nbsp;&nbsp;while elm = elm.next\_sibling  
&nbsp;&nbsp;&nbsp;&nbsp;break if elm.name != 'p'

&nbsp;&nbsp;&nbsp;&nbsp;puts funeral\_home  
&nbsp;&nbsp;&nbsp;&nbsp;puts "\t #{elm.inner\_text}"  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marc\_Farber](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@Marc\_Farber](https://rubytalk.org/u/Marc_Farber)\
**Post date:** [19 April 2009 23:50 UTC](https://rubytalk.org/t/hpricot-parsing/52943/5 "2009-04-19T23:50:12Z")

</div>

Thanks so much 7-stud

I had been fixated on next\_child thinking that next\_sibling would skip  
over the "p" tags. I really appreciate your thoughtfulness to provide a  
working code snippet.

Marc

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [20 April 2009 02:25 UTC](https://rubytalk.org/t/hpricot-parsing/52943/6 "2009-04-20T02:25:17Z")

</div>

Wang Jian wrote:

> Makes me wonder if ReXML, Hpricot or Nokogiri has a to\_hash method...not yet  
> found.

Try to write it. I hope I'm wrong, but I suspect that starting will be easy, and hitting your own target XML will be easy...

...but making it generic enough to publish will be another story!

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;Phlip
