Register now and start sharing your code snippets.
-->

Scraping Yahoo! Finance with Ruby and Hpricot

CSS posted 9 months ago by christian

This code extracts the numbers from the Fund operations table on the BLV fund’s Profile page at Yahoo! Finance.

   1  require 'rubygems'
   2  require 'hpricot'
   3  require 'open-uri'
   4  
   5  page = Hpricot(open('http://finance.yahoo.com/q/pr?s=BLV'))
   6  
   7  fund_operations = []
   8  page.search( "//table[@class='yfnc_datamodoutline1']" ).each do |row|
   9    row.search( "//td[@class='yfnc_datamoddata1']").each do |data|
  10      fund_operations << data.inner_html
  11    end
  12  end
  13  
  14  pp fund_operations

The output from this script is:

   1  ["N/A", "N/A", "55%", "72", "85.05M", "1.71B"]

Note that you could also use Scrubyt for this. Here’s a snippet that explains how to use Scrubyt to scrape web pages: Scraping Google search results with Scrubyt and Ruby

Tagged yahoo, finance, ruby, hpricot