Using Ruby & Hpricot to find lowest mortgage rate in Hawaii

By kenglish

Each week the Honolulu Board of Realtors publishes a report of Hawaii Mortgage Rates. To find the lowest rate for your category is difficult. A non-programming solution would be to copy it into excel, delete all the rows that you need and then sort by the rate column. This takes too much time so I wrote a ruby script that parses this data. This is also a demonstration of how to use the ruby tool Hpricot, an HTML Parser. You will need to install the Hpricot gem for this to work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'rubygems'
require 'hpricot'
require 'open-uri'
 
#term = '1-YR ARM'
#term = '30-YR Fixed'
term = '15-YR Fixed'
 
doc = Hpricot.parse(open("http://www.hicentral.com/MortgageRates.asp")) 
 
rates = [] 
lender_name = ""
 
(doc/"table"/"table"/"tr").each do | row |
   arr =[]
   (row/"td").each do | cell|  
     arr << cell.inner_html() 
   end
   if arr[1] =~ /15-YR Fixed/
      lender_name = arr[0]
      arr.delete(lender_name) 
      lender_name.sub!('<br />',' - ')
   end
   next unless arr[0] =~ /#{term}/
   lender_data ={} 
   lender_data[:lender_name] = lender_name    
   lender_data[:term] = arr[0] 
   lender_data[:apr] = arr[3].to_f
   rates << lender_data
end
 
5.times do | rank |
  puts "Losest Rate ##{rank+1}"
  row = rates.min{|a,b| a[:apr] <=> b[:apr] } 
  puts "  Name: #{row[:lender_name]}"  
  puts "  Term: #{row[:term]}" 
  puts "  APR: #{row[:apr]}"  
  rates.delete(row)
end

Here’s what’s going on:

  • On line 7 you will notice that I am interested in the 15-Year mortgage rate. You can change this value to get the report for the term you want.
  • On line 9, the program will download the latest rates from the hicentral.com website and parse the page returning an hpricot doc object.
  • From line 14 to 30, the program parses each line in the mortgage rate table. The logic is custom to this table. The table is unusual because the lender name is first cell only on the first line (15-YR Fixed) for each lender. To accommodate this, we match the line that has “15-YR Fixed” in second position and delete the lender name from the array (lines 19-23). We then assign the data to our summary data structure (lines 25-29)
  • Finally, we show the top 5 lowest mortgage rates (lines 32-38). To do this we use the Ruby max method (line 34). We delete the current max element so it is not counted in the next loop iteration (line 38).

Thomas Lecklider has a great tutorial on how to use Hpricot called Using Hpricot to Traverse and Parse HTML.

Your commends are welcome. Give some refactoring advice if you like. I have wordpress plugin for pre tag so to write ruby code just do:

<pre lang="ruby">
puts YEAH
</pre>



categoriaProgramming commento1 Comment dataDecember 14th, 2009

About... kenglish

This author published 76 posts in this site.

Share

FacebookTwitterEmailWindows LiveTechnoratiDeliciousDiggStumbleponMyspaceLikedin

Comments


kenglish
December 14th, 2009

Does this work

Hey bra

Leave a comment