- Author:
 - renatopedigoni
 - Posted:
 - August 1, 2008
 - Language:
 - Python
 - Version:
 - .96
 - 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
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
In English please.
#
@darek I think you mean:
Em inglês por favor.
:)
#
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..
#
aarond10ster
What should I do instead putting the hardcoded url? And what about reading the XML output?
Thanks
- darek / youell
Now in english :)
#
Hey renatopedigoni,
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.
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 ;)
#
Please login first before commenting.