From: Peter Szinek Date: 2007-12-01T18:07:35+09:00 Subject: Re: Need to parse a HTML table into an Array/hash --Apple-Mail-22--850474128 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit On Nov 30, 2007, at 10:24 PM, Ernst Tanaka wrote: > I am new to ruby; having a lot of fun, but currently I am stuck with > the > following. > > I want to read this page > > http://finance.yahoo.com/q/op?s=AAPL&k=95.00&m=2007-12 scRUBYt! is especially well suited for this type of task. Watch this: ================================================== require 'rubygems' require 'scrubyt' Scrubyt.logger = Scrubyt::Logger.new yahoo_data = Scrubyt::Extractor.define do fetch ' http://finance.yahoo.com/q/op?s=AAPL&k=95.00&m=2007-12' record do expires 'Dec 07' symbol 'QAALS.X' last '86.60' chg 'Down 3.00' bid '87.10' ask '87.50' vol '4' open_int '146' end.select_indices(1..6) end p yahoo_data.to_hash yahoo_data.export(__FILE__) ================================================== If you want to get just the first 3 results, change select_indices(1..3) output: ================================================== [{:chg=>"Dec 07QAALS.X86.60 3.0087.1087.504146", :bid=>"87.10", :ask=>"87.50", :vol=>"4", :last=>"86.60", :expires=>"Dec 07", :open_int=>"146", :symbol=>" QAALS.X"}, {:chg=>"Jan 08QAAAS.X88.10 0.9587.4587.901714,832", :bid=>"87.45", :ask=>"87.90", :vol=>"17", :last=>"88.10", :expires=>"Jan 08", :open_int=>"14,832", :symbol=>" QAAAS.X"}, {:chg=>"Apr 08QAADS.X88.35 2.1089.0089.4511973", :bid=>"89.00", :ask=>"89.45", :vol=>"11", :last=>"88.35", :expires=>"Apr 08", :open_int=>"973", :symbol=>" QAADS.X"}, {:chg=>"ExpiresSymbolLastChgBidAskVolOpen Int", :bid=>"Bid", :ask=>"Ask", :vol=>"Vol", :open_int=>"Open Int"}, {:chg=>"Dec 07QAAXS.X0.01 0.00N/A0.034128", :bid=>"N/A", :ask=>"0.03", :vol=>"4", :last=>"0.01", :expires=>"Dec 07", :open_int=>"128", :symbol=>"QAAXS.X"}, {:chg=>"Jan 08QAAMS.X0.10 0.040.070.112,38619,345", :bid=>"0.07", :ask=>"0.11", :vol=>"2,386", :last=>"0.10", :expires=>"Jan 08", :open_int=>"19,345", :symbol=>" QAAMS.X"}] ================================================== The very last line also creates a second file, which looks like this: ================================================== require 'rubygems' require 'scrubyt' yahoo_data = Scrubyt::Extractor.define do fetch(" http://finance.yahoo.com/q/op?s=AAPL&k=95.00&m=2007-12") record("/html/body/div/div/table/tr/td/table/tr/td/table/tr", { :generalize => true }) do expires("/td[1]/b[1]/a[1]") symbol("/td[2]/a[1]") last("/td[3]/b[1]") chg() bid("/td[5]") ask("/td[6]") vol("/td[7]") open_int("/td[8]") end.select_indices((1..6)) end yahoo_data.to_xml.write($stdout, 1) ================================================== which obviously works whatever the content of the page is. There is much more in sRUBYt!, this was just a very basic example, so if you need some aditional tweaking, just drop me a mail. HTH, Peter ___ http://www.rubyrailways.com http://scrubyt.org --Apple-Mail-22--850474128--