Skip to content

Instantly share code, notes, and snippets.

@manfe
Created October 23, 2012 16:39
Show Gist options
  • Save manfe/3939960 to your computer and use it in GitHub Desktop.
Save manfe/3939960 to your computer and use it in GitHub Desktop.
Recuperando Cidades de Estados Brasileiros do Site do IBGE
require 'nokogiri'
require 'htmlentities'
require 'open-uri'
estados = [ "ac", "al", "am", "ap", "ba", "ce", "df", "es", "go", "ma", "mg", "ms", "mt",
"pa", "pb", "pe", "pi", "pr", "rj", "rn", "ro", "rr", "rs", "sc", "se", "sp",
"to" ]
@result = Hash.new
class String
def titlecase
gsub(/(?:_|\b)(.)/){$1.upcase}
end
end
estados.each do |e|
doc = Nokogiri::HTML(open("http://www.ibge.gov.br/cidadesat/includes/#{e}.inc"))
doc = doc.xpath('//p')
variables = []
doc.each do |c|
variables = c.text.encode
end
variables.gsub! /\r\n/, ' '
variables.force_encoding('UTF-8')
estado = variables.match(/uf=(.*?);/)[1]
municipios = variables.match(/mundata=(.*?);/)[1]
municipios = eval(municipios)
municipios_new = []
municipios.each do |m|
municipios_new << [:nome => m[6].titlecase, :codigo_ibge => m[7]]
end
estado_hash = { estado.to_sym => municipios_new }
@result.merge! estado_hash
end
p @result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment