From: 7stud -- Date: 2009-04-20T08:20:43+09:00 Subject: Re: hpricot parsing Marc Farber wrote: > Ruby newbie here > > Have successfully used hpricot to scrape correct
from desired page > 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 > >
>

name of funeral home

>

deceased1

>
advertising crap
>

funeral home 2

>

deceased 2

>

deceased 3

>
> > 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 =<

name of funeral home

deceased1

advertising crap

funeral home 2

deceased 2

deceased 3

ENDOFSTRING doc = Hpricot(str) h3_tags = doc.search("h3") h3_tags.each do |h3| elm = h3 while elm = elm.next_sibling break if elm.name != 'p' puts h3.inner_text puts "\t #{elm.inner_text}" end end --output:-- name of funeral home deceased1 funeral home 2 deceased 2 funeral home 2 deceased 3 -- Posted via http://www.ruby-forum.com/.