# Hpricot getting a table

**URL:** <https://rubytalk.org/t/hpricot-getting-a-table/37018>\
**Category:** ruby-talk\
**Created:** [18 April 2007 15:45 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018 "2007-04-18T15:45:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrlebron](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lrlebron](https://rubytalk.org/u/lrlebron)\
**Post date:** [18 April 2007 15:45 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018/1 "2007-04-18T15:45:06Z")

</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|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts data  
end

At this point data contains html that looks like this

\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>

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:** ![Drew\_Raines1](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Drew\_Raines1](https://rubytalk.org/u/Drew_Raines1)\
**Post date:** [18 April 2007 16:15 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018/2 "2007-04-18T16:15:04Z")

</div>

lrlebron@gmail.com wrote:

> At this point data contains html that looks like this
> 
> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>
> 
> 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.

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

&nbsp;&nbsp;&nbsp;#!/usr/bin/env ruby

&nbsp;&nbsp;&nbsp;require "hpricot"

&nbsp;&nbsp;&nbsp;doc = Hpricot("\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
&nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
&nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
&nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>")

&nbsp;&nbsp;&nbsp;(doc/"table").map {|t| puts t.to\_html}

This outputs:

&nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
&nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
&nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
&nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"

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:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [18 April 2007 19:28 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018/3 "2007-04-18T19:28:40Z")

</div>

lrlebron@gmail.com wrote:

> 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|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts data  
> end
> 
> At this point data contains html that looks like this
> 
> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>
> 
> 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.

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:** ![lrlebron](https://avatars.discourse-cdn.com/v4/letter/l/a8b319/32.png) [@lrlebron](https://rubytalk.org/u/lrlebron)\
**Post date:** [18 April 2007 18:55 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018/4 "2007-04-18T18:55:07Z")

</div>

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

> **···**
>
> On Apr 18, 11:11 am, Drew Raines \<aarai...@gmail.com\> wrote:
> 
> > lrleb...@gmail.com wrote:  
> > \> At this point data contains html that looks like this
> > 
> > \> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > \> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > \> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > \> \<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>
> > 
> > \> 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.
> > 
> > Would something as simple as this work? I'm not sure how complex  
> > your tables get.
> > 
> > &nbsp;&nbsp;&nbsp;#!/usr/bin/env ruby
> > 
> > &nbsp;&nbsp;&nbsp;require "hpricot"
> > 
> > &nbsp;&nbsp;&nbsp;doc = Hpricot("\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > &nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > &nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>  
> > &nbsp;&nbsp;&nbsp;\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>")
> > 
> > &nbsp;&nbsp;&nbsp;(doc/"table").map {|t| puts t.to\_html}
> > 
> > This outputs:
> > 
> > &nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
> > &nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
> > &nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"  
> > &nbsp;&nbsp;&nbsp;"\<table\>\<tr\>\<td\>Stuff\</td\>\</tr\>\<tr\>\<td\>Stuff\</td\>\</tr\>\</table\>"
> > 
> > Note that there's an Hpricot mailing list athttp://code.whytheluckystiff.net/hpricot/that might be a more  
> > appropriate forum for these questions.
> > 
> > -Drew

---

<div class="post-metadata">

**Author:** ![Drew\_Raines1](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Drew\_Raines1](https://rubytalk.org/u/Drew_Raines1)\
**Post date:** [18 April 2007 20:00 UTC](https://rubytalk.org/t/hpricot-getting-a-table/37018/5 "2007-04-18T20:00:06Z")

</div>

lrlebron@gmail.com 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:

&nbsp;&nbsp;(doc/"table")

is much nicer than:

&nbsp;&nbsp;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 \<table\>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:

&nbsp;&nbsp;\<table class="foo"\>...\</table\>  
&nbsp;&nbsp;\<table class="foo"\>...\</table\>  
&nbsp;&nbsp;\<table class="foo"\>...\</table\>

Then do:

&nbsp;&nbsp;(doc/"table.foo")

That'll work regardless of how many \<table\>s are on the page.

-Drew

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