Hpricot fun

May 7th, 2007

rq.rb

%w(rubygems hpricot open-uri).each{|f| require f }
T = %w(goog nyx lmc fwlt nly aapl amzn vg)
scrape = lambda{|t| t.inner_html.gsub(/ |(|)/, '').strip }
doc = Hpricot(open("http://finance.google.com/finance?q=#{T.join('+')}"))
doc.search('table.results tr').each_with_index do |row, i|
  next if i == 0 # header
  tds = row/'td'
  s = scrape[tds[1]/'a']
  p = scrape[tds[2]]
  dc = scrape[tds[3]/'font']
  pc = scrape[tds[4]/'font']
  puts sprintf("%5s %10s %8s %8s", s, p, dc, pc)
end

ruby rq.rb

 GOOG     471.26    +0.14    0.03%
  NYX      83.70    -0.71   -0.84%
  LMC      13.11    +0.21    1.63%
 FWLT      71.21    +0.70    0.99%
  NLY      15.87    +0.12    0.75%
 AAPL     103.04    +2.23    2.21%
 AMZN      62.25    -0.98   -1.55%
   VG       3.09    -0.03   -0.96%
--- --- ---

1 Comment

  1. Comment by sandro on 05/13/07
    Mmh... what extacly do the class Hpricot?

Commenting is closed for this article.