Last active
January 4, 2017 12:48
-
-
Save LuisFcoOrtiz/98105458f0603cffd7c1d1e65e7736d6 to your computer and use it in GitHub Desktop.
Python parsing wheather XML | Python leyendo XML de AEMET tiempo atmosferico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# tiempoAtmosferico.py | |
# | |
# Copyright 2016 manrique <https:/github.com/luisFcoOrtiz> | |
# | |
from xml.dom import minidom | |
import urllib | |
#Introduce el XML de la direccion URL en una variable | |
url = urllib.urlopen('http://www.aemet.es/xml/municipios/localidad_28006.xml') | |
doc = minidom.parse(url) | |
name = doc.getElementsByTagName("nombre")[0] | |
enlace = doc.getElementsByTagName("enlace")[0] | |
print("\nTiempo atmosferico en: " + name.firstChild.data) | |
print ("Mas informacion detallada en: " + enlace.firstChild.data + "\n") | |
etiquetaDia = doc.getElementsByTagName("dia") | |
#Recorre cada elemento dentro de la etiqueta <dia> | |
for dia in etiquetaDia: | |
#Recogida de atributos (fecha,probabilidad prec, temp max, temp min) | |
fecha = dia.getAttribute("fecha") | |
probPrecip = dia.getElementsByTagName("prob_precipitacion")[0] | |
tempMax = dia.getElementsByTagName("maxima")[0] | |
tempMin = dia.getElementsByTagName("minima")[0] | |
#comprobacion | |
try: | |
#impresion por consola | |
print("\nfecha: %s | prob general lluvia: %s | temp Max:%s | temp Min:%s" % (fecha, probPrecip.firstChild.data, tempMax.firstChild.data, tempMin.firstChild.data)) | |
#pasa a numero entero la salida de probabilidad de precipitacion | |
precipitacion = int(probPrecip.firstChild.data) | |
#Comprueba la probabilidad de precipitacion | |
if ( precipitacion == 100): | |
print "Probabilidad de lluvia COMPLETA: " | |
elif (precipitacion >= 80): | |
print "Probabilidad de lluvia MUY ALTA: " | |
elif (precipitacion < 80 and precipitacion >= 50): | |
print "Probabilidad de lluvia ALTA: " | |
elif (precipitacion < 50 and precipitacion >= 20): | |
print "Probabilidad de lluvia BAJA: " | |
elif (precipitacion < 20 and precipitacion >= 0): | |
print "Probabiliad de lluvia ESCASA: " | |
print "----------------------------------------------------------------------" | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment