我正在尝试在我的模型上存储来自msn天气服务的XML文件数据,因为您可以看到下面的代码
class Command(NoArgsCommand):
help = "This will update the weather reports by fetching xml from msn and storing data"
def handle_noargs(self, **options):
tree = ElementTree()
path = urllib2.urlopen('http://weather.service.msn.com/data.aspx?src=vista&weadegreetype=C&culture=en-US&wealocations=wc:sfxx0010')
tree = ET.parse(path)
root = tree.getroot()
xmllist=[]
a = 0
for i in root:
xmllist = xmllist + [XmlDictConfig(i)]
for i in xmllist:
date = fixdate(i['date']) #Date, date=date heading=i['heading']
a = Forecast.objects.create(mintemp=i['low'] ,maxtemp=i['high'],wind=i['wind'],rain=i['precipitation'],date=date)
a.save()
print "Data Updated"
我收到了一个关键错误 KeyError:'date'
答案 0 :(得分:0)
我管理工作,它花了一个不眠之夜,但它就像下面的代码一样简单
class Command(NoArgsCommand):
help = "This will update the weather reports by fetching xml from msn and storing data"
def handle_noargs(self, **options):
path = urllib2.urlopen('http://weather.service.msn.com/data.aspx?src=vista&weadegreetype=C&culture=en-US&wealocations=wc:sfxx0010')
tree = ET.parse(path)
root = tree.getroot()
x = root.findall('*/forecast')
for forecast in x:
date = forecast.get('date') #Date, date=date heading=i['heading'], ,maxtemp=i['high'],wind=i['wind'],rain=i['precipitation']
maxtemp = forecast.get('high')
wind = forecast.get('skytextday')
rain = forecast.get('precip')
mintemp = forecast.get('low')
a = Forecast.objects.create(mintemp=mintemp, maxtemp=maxtemp, date=date, wind=wind, rain=rain)
a.save()
print "Data Updated Thank You"