# Using HPricot to parse a fiddly table

**URL:** <https://www.ruby-forum.com/t/using-hpricot-to-parse-a-fiddly-table/125185>\
**Category:** Ruby\
**Created:** [January 6, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/using-hpricot-to-parse-a-fiddly-table/125185 "2008-01-06T20:13:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [January 6, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/using-hpricot-to-parse-a-fiddly-table/125185/1 "2008-01-06T20:13:28Z")

</div>

Hi there,

I’m fairly new to Ruby, previously I was an average programmer in Java,  
so it’s all a bit foreign to me - especially XPath and cSS. I would be  
grateful if someone could give me a hand with a problem I’m having. I  
have a table which I’m trying to get the fields from in a certain way.  
The table is in the form:

| ...stuff I don't want... |
| ------------rows i want ------------end of rows i want 

| 

| Field 1 | Field 2 |

 | Field 3 | Field 4, Field 5 |

 |

I have managed to get HPricot to parse the page and return that HTML for  
the table, however I’m struggling to get it into an array in the form  
[“Field 1”, “Field 2”, “Field 3”, “Field 4”, “Field 5”] for each row. I  
would have hoped there would be some kind of built in method for  
extracting data from a table, but I can’t find one.

Thanks again, look forward to a reply 🙂  
Adam

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [January 6, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/using-hpricot-to-parse-a-fiddly-table/125185/2 "2008-01-06T21:40:02Z")

</div>

For the innermost table, try:

eles = doc.search(‘table table table td’)

for the enclosing table,

eles = doc.search(‘table table td’)

I don’t suppose the semantics can be improved any – like class names  
or ids?

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [January 7, 2008, 1:49am UTC](https://www.ruby-forum.com/t/using-hpricot-to-parse-a-fiddly-table/125185/3 "2008-01-07T01:49:29Z")

</div>

Steve R. wrote:

> I don’t suppose the semantics can be improved any – like class names  
> or ids?

Thanks for your reply. Afraid not, no handy names or ids. The code you  
posted I think I was doing anyway in a slightly different form as  
“elements2 = (elements/“table//table//td”)”. Since I posted last though  
I’ve managed to sort it out just by lots of array manipulation.

Thanks for the help though 🙂  
Adam
