- 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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 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.