Oppure

Loading
21/08/11 14:24
RedJohn
Ciao a tutti.
Ho scritto un programma che ti dice il tempo nella città da te inserita usando le API di Google. L'ho scritto come esercizio per verificare le mie capacità di usare file XML con python. Vorrei avere le vostre considerazioni e/o critiche. Magari poi lo posto nei programmi della community (dopo aver aggiunto un'interfaccia grafica :heehee: )

# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET
import urllib

class CurrentCondition(object):
    def __init__(self,city=""):
        # Set up the city, the condition and the temperatur in C°
        self.city = city
        self.condition = ""
        self.temp_c = ""
        
        # Get the XML file from the Google's API
        self.xmlFile = urllib.urlopen("http://www.google.com/ig/api?weather="+self.city)
        
        # Parse the XML file
        self.tree = ET.parse(self.xmlFile)
        
    def getCondition(self):
        # Get the name of the city, the condition and the temp_c
        for subelement in self.tree.getroot():
            for subelement in subelement:
                for subelement in subelement:
                    if subelement.tag == "city":
                        self.city = subelement.attrib["data"]
                        
                    elif subelement.tag == "condition":
                        self.condition = subelement.attrib["data"]
                        
                    elif subelement.tag == "temp_c":
                        self.temp_c = subelement.attrib["data"]
                        
        # Print the weather condition
        print "City: %s" %(self.city)
        print "Condition: %s" %(self.condition)
        print "Temperature in C: %s" %(self.temp_c)
        
if __name__ == "__main__":
    current = CurrentCondition(raw_input("Type the name of your city or your zip code -->"))
    current.getCondition()


Ultima modifica effettuata da RedJohn 22/08/11 12:28
aaa
25/08/11 13:40
cavolo, qua ci sono 37 gradi!:yup:
25/08/11 13:42
solo una cosa, se metti una città che non esiste lascia i campi vuoti!
piuttosto fai in modo che scriva tipo "città non trovata" o una roba così