# Hpricot getting a table

**URL:** <https://www.ruby-forum.com/t/hpricot-getting-a-table/92357>\
**Category:** Ruby\
**Created:** [April 18, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357 "2007-04-18T17:45:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357/1 "2007-04-18T17:45:27Z")

</div>

I am currently trying to scrape some data from the following web page

I am using some hpricot code that looks like this  
@doc = Hpricot(open(strLink))

@doc.search("/html/body/table[5]/tr/td[2]/div[3]/table/tr/td/div[1]/  
table") do |data|  
puts data  
end

At this point data contains html that looks like this

| Stuff |
| Stuff |

| Stuff |
| Stuff |

| Stuff |
| Stuff |

| Stuff |
| Stuff |

More stuff continues …

I want capture each of these four tables individually for further  
processing. I have tried a variety of methods but nothing seems to  
work.

Thanks,  
Luis

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357/2 "2007-04-18T18:15:35Z")

</div>

removed\_email\_address@domain.invalid wrote:

> processing. I have tried a variety of methods but nothing seems to  
> work.

Would something as simple as this work? I’m not sure how complex  
your tables get.

#!/usr/bin/env ruby

require “hpricot”

doc =  
Hpricot("

| Stuff |
| Stuff |

| Stuff |
| Stuff |

| Stuff |
| Stuff |

| Stuff |
| Stuff |

")

(doc/“table”).map {|t| puts t.to\_html}

This outputs:

“

| Stuff |
| Stuff |

”  
“

| Stuff |
| Stuff |

”  
“

| Stuff |
| Stuff |

”  
“

| Stuff |
| Stuff |

”

Note that there’s an Hpricot mailing list at  
[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/) that might be a more  
appropriate forum for these questions.

-Drew

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357/3 "2007-04-18T20:55:51Z")

</div>

On Apr 18, 11:11 am, Drew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I want capture each of these four tables individually for further  
> > doc = Hpricot("
> > 
> > | Stuff |
> > | Stuff |
> > 
> >   
> > “
> > 
> > | Stuff |
> > | Stuff |
> > 
> > ”  
> > “
> > 
> > | Stuff |
> > | Stuff |
> > 
> > ”
> 
> Note that there’s an Hpricot mailing list athttp://code.whytheluckystiff.net/hpricot/that might be a more  
> appropriate forum for these questions.
> 
> -Drew

Thanks,

This gets me a lot closer to what I need.  
I’m having some problems with syntax. If I’m reading the docs  
correctly map returns an array. So I should be able to do something  
like

arrTables = (doc/“tables”).map

And then access each table individually. For example

arrTables[0]

Luis

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357/4 "2007-04-18T21:29:54Z")

</div>

removed\_email\_address@domain.invalid wrote:

> At this point data contains html that looks like this  
> work.  
> What are you trying to do exactly? What should be the result?  
> Could you please provide some real data, because these ‘stuff’ do not  
> make too much sense 🙂

Thanks,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 18, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/hpricot-getting-a-table/92357/5 "2007-04-18T22:00:58Z")

</div>

removed\_email\_address@domain.invalid wrote:

> This gets me a lot closer to what I need.  
> I’m having some problems with syntax. If I’m reading the docs  
> correctly map returns an array. So I should be able to do something  
> like
> 
> arrTables = (doc/“tables”).map
> 
> And then access each table individually. For example
> 
> arrTables[0]

Array#map wasn’t particularly relevant in my example; I just used it  
to iterate #puts over the result of (doc/“table”).

The real lesson to glean from my response is that:

(doc/“table”)

is much nicer than:

doc.search(“/html/body/table[5]/tr/td[2]/div[3]/table/tr/td/div[1]/table”)

…which, like Peter alluded to, is fairly meaningless to us because  
we don’t know what the full original HTML looks like. You can grab  
the

s from the snippet you provided with just a CSS-style  
search[1].

FWIW, if you have any control over the markup, you can simply add  
some unique classes to the tables you want:

...

 ...

 ...

Then do:

(doc/“table.foo”)

That’ll work regardless of how many

s are on the page.

-Drew

Footnotes:  
[1] [http://lnk.nu/code.whytheluckystiff.net/e7m](http://lnk.nu/code.whytheluckystiff.net/e7m)
