Skip to content

Instantly share code, notes, and snippets.

@mvrilo
Created September 21, 2011 20:41
Show Gist options
  • Save mvrilo/1233240 to your computer and use it in GitHub Desktop.
Save mvrilo/1233240 to your computer and use it in GitHub Desktop.
cep search scrapping
require 'rubygems'
require 'nokogiri'
require 'net/http'
require 'uri'
target = 'http://www.buscacep.correios.com.br/servicos/dnec/consultaEnderecoAction.do'
if ARGV[0].nil?
puts "USAGE:"
puts " ruby cep.rb xxxxxxxx"
exit
end
search = {:relaxation => ARGV[0] || "01048000",
:TipoCep => "ALL",
:semelhante => "N",
:cfm => "1",
:Metodo => "listaLogradouro",
:TipoConsulta => "relaxation",
:StartRow => "1",
:EndRow => "10"}
correios = Nokogiri::HTML(Net::HTTP.post_form(URI.parse(target), search).body)
data = correios.search('.content .ctrlcontent div td')
record = {}
record["address"] = data[0].text.split(" - ")[0]
record["city"] = data[2].text
record["state"] = data[3].text
record["zipcode"] = data[4].text
puts record.inspect
# based on https://scraperwiki.com/scrapers/cep/edit/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment