require 'open-uri' require 'yaml' h={} (1..267).each do |x| open("http://www.couchsurfing.org/get_option_values.html?cpaint_function=get_states_auto&cpaint_argument[]=#{x}") do |f| if f.first # anybody knows why it doesnt check if the string is empty before it executes the split ??? # I went through all x which dont work and substitude f.first with # x!=112 and x!=122 and x!=124 and x!= 150 and x!=219 and x!= 240 # which works. Pretty weird! f.first.split('#').collect{|i| i.split('|')}.each do |i| h["state_#{i[0]}"]={} h["state_#{i[0]}"]["id"] = i[0] h["state_#{i[0]}"]["state_id"] = i[1] h["state_#{i[0]}"]["country_id"] = x puts "country_id: #{x}, state_id: #{i[0]}, state: #{i[1]}" end end end end File.open('states.yml', 'w') do |f| f.write(h.to_yaml) end