Login

Brazillian freight calculator (Correios)

Author:
renatopedigoni
Posted:
August 1, 2008
Language:
Python
Version:
.96
Tags:
correios freight
Score:
-2 (after 4 ratings)

This snippet calculates Brazillian freight through Correios website.

  • parameter ´peso´(weight) measured in grams
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import urllib
from django.conf import CEP_LOJA
from xml.dom import minidom

URL_CORREIOS = 'http://www.correios.com.br/encomendas/precos/calculo.cfm?servico=40010&CepOrigem=%s&CepDestino=%s&Peso=%f&ValorDeclarado=0&resposta=xml' 
		
def calcularFrete(cepDestino, peso):
	url = URL_CORREIOS % (CEP_LOJA, cepDestino, peso/1000.0)
	dom = minidom.parse(urllib.urlopen(url))
	codigoErro = dom.getElementsByTagName("codigo")[0].childNodes[0].data
	if codigoErro=='0':
		return dom.getElementsByTagName("preco_postal")[0].childNodes[0].data
	return False

More like this

Comments

darek (on August 1, 2008):
<p>In English please.</p>

#

youell (on August 3, 2008):
<p>@darek I think you mean: </p> <p>Em inglês por favor.</p> <p>:)</p>

#

aarond10ster (on August 3, 2008):
<p>Looks like a Brazillian freight calculator at a guess. peso means weight. Still, not really conforming to django DRY. That URL shouldn't be hard coded and the screen/DOM scraping is a little short of robust..</p>

#

renatopedigoni (on August 3, 2008):
<p>aarond10ster</p> <p>What should I do instead putting the hardcoded url? And what about reading the XML output?</p> <p>Thanks</p> <p>- darek / youell</p> <p>Now in english :)</p>

#

aarond10ster (on August 5, 2008):
<p>Hey renatopedigoni,</p> <p>Sorry, I just re-read my post. I wasn't trying to sound too negative. Its a good contribution! The service you are using is not designed to be used like this so I was just pointing out that it might be a little bit brittle if used on a big site.</p> <p>About the URL, its a style thing but for me, if the URL doesn't change, it should probably be inside the function or if it does change, it should be in settings.URL_CORREIOS. I don't like global variables - even with python file-level namespaces ;)</p>

#

Please login first before commenting.