1 require "open-uri"
2 require "nokogiri"
3 require "csv"
4
5 years = (2006..2010)
6 mileages = (0..10).map{ |v| 10000 + v*5000 }
7
8 data = {}
9
10 years.each do |year|
11 data[year] = {}
12 mileages.each do |mileage|
13 sleep 2
14 doc = Nokogiri::HTML(open("http://www.kbb.com/mazda/mazda5/#{year}-mazda-mazda5/sport-minivan-4d/?pricetype=private-party&anchor=true&mileage=#{mileage}"))
15 data[year][mileage] = doc.css(".good-value .value").text
16 puts "year: #{year} mileage: #{mileage} value: #{data[year][mileage]}"
17 end
18 end
19
20 CSV.open("mazda5.csv", "w") do |csv|
21 csv << [""].concat(years.to_a)
22 mileages.each do |mileage|
23 csv << [mileage].concat(data.values.map{ |y| y[mileage] })
24 end
25 endThis produced a nice spreadsheet file and then I created the following chart (y axis is cost, x axis is mileage):
Interesting observations:
The car seems to depreciate at the same rate with respect to miles, regardless of the year. (ie, the slope of the lines are all about the same)
The car seems to depreciate at about the same rate with respect to time for the first few years. (ie, the space between lines is constant for 2010,2009,2008)
There is a big drop in value between 2008 and 2007 models, and a slightly smaller gap between 2007 and 2006 models.
Conclusions:
1) I like ruby. The library I used, nokogiri, is great. I've tried some python libraries, like beautiful soup and lxml, but none are as succinct as nokogiri, especially when it comes to css selectors.
2) I find it interesting that there is such a sharp drop between 2008 and 2007. I always thought the car dropped in value more during the first 3 years than in years 4-8.
3) In the first three years, depreciation is about $500/year and $500/10k miles. So if trends continued, you could buy a 2010 and sell the car after 2 years and 20k for 2000 less than you bought it for.
