Created
January 3, 2012 18:47
-
-
Save fahim/1556275 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
namespace :tax_rates_scraper do | |
namespace :california do | |
# Order to run the rake tasks: | |
# rake tax_rates_scraper:california:tax_rates | |
# rake tax_rates_scraper:california:zip_codes | |
task :zip_codes => [:environment] do | |
url = "http://www.melissadata.com/lookups/CountyZip.asp?State=CA06California" | |
file = open(url) | |
page = file.read | |
page.scan(/<option value="(06[0-9]+)">([a-z\s]+)<\/option>/i).each do |match| | |
url_end = match[0] | |
county = match[1] | |
# Download each County file to db/CA/#{county}.html | |
county_url = "http://www.melissadata.com/lookups/CountyZip.asp?fips=" + url_end | |
save_loc = Rails.root.join("db/zip_codes/CA/#{county}.html") | |
download_url(county_url, save_loc) | |
puts "Saved #{save_loc}" | |
end | |
end | |
def download_url(from, to) | |
open(to, 'wb') { |file| file << open(from).read } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment