Scraping 3rd element with hpricot

M

Mark Nielsen

I don't know why I'm struggling with this (not a Ruby expert) but using
the standard scraping example:

doc = Hpricot(@response)
puts (doc/"#{xpath}").first.inner_html
puts (doc/"#{xpath}").last.inner_html

What could I use instead of first or last to get the nth element?

Thanks!
 
M

Michael Libby

I don't know why I'm struggling with this (not a Ruby expert) but using
the standard scraping example:

doc = Hpricot(@response)
puts (doc/"#{xpath}").first.inner_html
puts (doc/"#{xpath}").last.inner_html

What could I use instead of first or last to get the nth element?

Hpricot::Doc#search returns an instance Hpricot::Elements, which is a
subclass of Array, so you can index it like any other array.

puts (doc/"#{xpath}")[n].inner_html # we're counting from 0 here, so
to get the 3rd element n=2

However, you will get an error with this if there are not enough
elements returned by the search. So it might be better to do the
search, validate that there are as many elements as you expect and
then get the element you want by index. Like so:

puts x.inner_html if x = (doc/"#{xpath}")[n]

-Michael
 
M

Mark Nielsen

Michael said:
puts (doc/"#{xpath}")[n].inner_html # we're counting from 0 here, so
to get the 3rd element n=2

Thanks Michael, sometimes it's right in front of you. I was trying to
add a dot before [n]...

--Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top