# HTML table to matrix with WWW::Mechanize

**URL:** <https://www.ruby-forum.com/t/html-table-to-matrix-with-www-mechanize/75726>\
**Category:** Ruby\
**Created:** [November 12, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/html-table-to-matrix-with-www-mechanize/75726 "2006-11-12T20:15:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [November 12, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/html-table-to-matrix-with-www-mechanize/75726/1 "2006-11-12T20:15:07Z")

</div>

Hi,

I am new to ruby and am trying to scrape a website table into a matrix,  
I have been playing around with WWW::Mechanize and have had some success  
getting the page, extracting the table I want and then separating the  
result by table rows. The problem comes with then splitting it down by  
table data, I am going through each result in the array to break it by  
the tag, but my code appears to have zero effect!

The code I am using is below; any help in getting the table into a  
matrix would be really appreciated.

Thanks

Adam

require ‘rubygems’  
require ‘mechanize’

agent = WWW::Mechanize.new  
agent.user\_agent\_alias = ‘Mac Safari’  
page =  
agent.get(“[http://horses.sportinglife.com/Racecards/0,12495,215137,00.html").search("//table[@class=‘racecard\_table’]](http://horses.sportinglife.com/Racecards/0,12495,215137,00.html%22).search(%22//table%5B@class='racecard_table'%5D)”)  
tablerows = page.search(“//tr”)  
puts tablerows.length  
finalresult = Array.new  
tablerows.each do |tablerows|  
finalresult \<\< tablerows.search(“//td”)  
end  
puts finalresult.length

---

<div class="post-metadata">

**Author:** ![omgitsads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/dbc845/32.png) [@omgitsads](https://www.ruby-forum.com/u/omgitsads)\
**Post date:** [November 12, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/html-table-to-matrix-with-www-mechanize/75726/2 "2006-11-12T20:30:56Z")

</div>

Adam Hinchliffe wrote:

> matrix would be really appreciated.  
> / … snip code listing

Try this:

* * *

#!/usr/bin/ruby -w

table = “

\n” +  
“\n” +  
“\n” +  
“\n” +  
“

| 4 | 47 | 1 | 19 |
| 7 | 49 | 4 | 39 |
| 14 | 17 | 19 | 21 |

\n”

rows = table.scan(%r{

.\*?}m)
## rows.each do |row| fields = row.scan(%r{(.\*?)}m) puts fields.join(",") end

Output:

4,47,1,19  
7,49,4,39  
14,17,19,21

Try this filter on a more complex page, one with differing numbers of  
cells  
in each row, etc. It works quite well, and you can understand what it is  
doing at a glance.

And notice that most of the listing is sample data, the actual filter  
code  
consists of five lines of Ruby.

The important thing to remember about Ruby is that actually writing code  
is  
so efficient that it is hard to justify applying a predefined package to  
some of the simpler processing tasks.
