# Scraping \<table\> from a website

**URL:** <https://rubytalk.org/t/scraping-table-from-a-website/42337>\
**Category:** ruby-talk\
**Created:** [19 November 2007 18:45 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337 "2007-11-19T18:45:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cskilbeck](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@cskilbeck](https://rubytalk.org/u/cskilbeck)\
**Post date:** [19 November 2007 18:45 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/1 "2007-11-19T18:45:00Z")

</div>

Hi,

I need to extract everything between \<table\> and \</table\> on a website  
(there's only one table on the page. So far I have:

require 'open-uri'  
page = open('[http://xxx.html](http://xxx.html)').read  
page.gsub!(/\n/,"")  
page.gsub!(/\r/,"")  
inner = page.scan(%r{.\*\<table.\*\>(.\*)\</table\>.\*}m)  
print inner

but inner is empty - any ideas?

If I substitute line 2 with

page = '123\<table\>456\</table\>789

I get inner = 456, which is correct.

---

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://rubytalk.org/u/A_LeDonne)\
**Post date:** [19 November 2007 18:55 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/2 "2007-11-19T18:55:51Z")

</div>

> Hi,
> 
> I need to extract everything between \<table\> and \</table\> on a website  
> (there's only one table on the page. So far I have:
> 
> require 'open-uri'  
> page = open('[http://xxx.html](http://xxx.html)').read  
> page.gsub!(/\n/,"")  
> page.gsub!(/\r/,"")  
> inner = page.scan(%r{.\*\<table.\*\>(.\*)\</table\>.\*}m)

Untested, but try:

inner = page.scan(%r{.\*\<table[^\>]\*\>(.\*)\</table\>.\*}m)

> print inner
> 
> but inner is empty - any ideas?
> 
> If I substitute line 2 with
> 
> page = '123\<table\>456\</table\>789
> 
> I get inner = 456, which is correct.

If you try page = '123\<table\>\<tr\>\<td\>456\</td\>\</tr\>\</table\>789', it  
will fail again.

You only want to capture up to the next closing angle bracket. What's  
happening is that the second .\* is matching the contents of the entire  
table, up to the closing angle bracket of the last tag (probably  
\</tr\>) right before the \</table\>, and inner gets only the leftover  
whitespace inbetween. So only capture characters that are NOT a  
closing angle bracket.

-Alex

> **···**
>
> On Nov 19, 2007 1:45 PM, cskilbeck \<charlieskilbeck@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Rolando\_Abarca](https://avatars.discourse-cdn.com/v4/letter/r/5daacb/32.png) [@Rolando\_Abarca](https://rubytalk.org/u/Rolando_Abarca)\
**Post date:** [19 November 2007 19:00 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/3 "2007-11-19T19:00:35Z")

</div>

use the right tools for the right job 🙂

require 'hpricot'  
require 'open-uri'

doc = Hpricot(open('[http://xxx.html](http://xxx.html)'))  
table = doc.at('table')  
puts table.inner\_html

(not tested)  
regards,

> **···**
>
> On Nov 19, 2007, at 3:45 PM, cskilbeck wrote:
> 
> > Hi,
> > 
> > I need to extract everything between \<table\> and \</table\> on a website  
> > (there's only one table on the page. So far I have:
> > 
> > require 'open-uri'  
> > page = open('[http://xxx.html](http://xxx.html)').read  
> > page.gsub!(/\n/,"")  
> > page.gsub!(/\r/,"")  
> > inner = page.scan(%r{.\*\<table.\*\>(.\*)\</table\>.\*}m)  
> > print inner
> > 
> > but inner is empty - any ideas?
> > 
> > If I substitute line 2 with
> > 
> > page = '123\<table\>456\</table\>789
> > 
> > I get inner = 456, which is correct.
> 
> --  
> Rolando Abarca  
> Phone: +56-9 97851962

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [19 November 2007 19:15 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/4 "2007-11-19T19:15:02Z")

</div>

inner = page[%r{\<table.\*?\>(.\*?)\</table\>}mi, 1]

> **···**
>
> On Nov 19, 12:41 pm, cskilbeck \<charlieskilb...@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I need to extract everything between \<table\> and \</table\> on a website  
> > (there's only one table on the page. So far I have:
> > 
> > require 'open-uri'  
> > page = open('[http://xxx.html](http://xxx.html)').read  
> > page.gsub!(/\n/,"")  
> > page.gsub!(/\r/,"")  
> > inner = page.scan(%r{.\*\<table.\*\>(.\*)\</table\>.\*}m)  
> > print inner
> > 
> > but inner is empty - any ideas?
> > 
> > If I substitute line 2 with
> > 
> > page = '123\<table\>456\</table\>789
> > 
> > I get inner = 456, which is correct.

---

<div class="post-metadata">

**Author:** ![cskilbeck](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@cskilbeck](https://rubytalk.org/u/cskilbeck)\
**Post date:** [19 November 2007 21:00 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/5 "2007-11-19T21:00:15Z")

</div>

Thanks all for your help. non greedy matching is the key.

> **···**
>
> On Nov 19, 7:14 pm, William James \<w\_a\_x\_...@yahoo.com\> wrote:
> 
> > On Nov 19, 12:41 pm, cskilbeck \<charlieskilb...@gmail.com\> wrote:
> > 
> > \> Hi,
> > 
> > \> I need to extract everything between \<table\> and \</table\> on a website  
> > \> (there's only one table on the page. So far I have:
> > 
> > \> require 'open-uri'  
> > \> page = open('[http://xxx.html](http://xxx.html)').read  
> > \> page.gsub!(/\n/,"")  
> > \> page.gsub!(/\r/,"")  
> > \> inner = page.scan(%r{.\*\<table.\*\>(.\*)\</table\>.\*}m)  
> > \> print inner
> > 
> > \> but inner is empty - any ideas?
> > 
> > \> If I substitute line 2 with
> > 
> > \> page = '123\<table\>456\</table\>789
> > 
> > \> I get inner = 456, which is correct.
> > 
> > inner = page[%r{\<table.\*?\>(.\*?)\</table\>}mi, 1]

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [20 November 2007 07:14 UTC](https://rubytalk.org/t/scraping-table-from-a-website/42337/6 "2007-11-20T07:14:42Z")

</div>

Amazing -- I thought that the above would be a massive project, not what  
appears to be pseudo-code! Not everything in Ruby is magically easy, but  
the above is pretty good 🙂

-Thufir

> **···**
>
> On Tue, 20 Nov 2007 04:00:35 +0900, Rolando Abarca wrote:
> 
> > require 'hpricot'  
> > require 'open-uri'
> > 
> > doc = Hpricot(open('[http://xxx.html](http://xxx.html)')) table = doc.at('table')  
> > puts table.inner\_html
