#-*-coding=utf-8-*- 'weather report sax parser' __author__='lixiaolin' from xml.parsers.expat import ParserCreate L=[] count=0 class WeatherSaxHandler(object): count=0 def start_element(self,name,attrs): global L,count if name=='yweather:location': L.insert(0,attrs['city']) L.insert(1,attrs['country']) if name=='yweather:forecast' and count==0: count=count+1 L.insert(2,attrs['text']) L.insert(3,attrs['low']) L.insert(4,attrs['high']) if name=='yweather:forecast' and count==1: L.insert(5,attrs['text']) L.insert(6,attrs['low']) L.insert(7,attrs['high']) def end_element(self,name): pass def char_data(self,text): pass def result(self): global L return { 'city': L[0], 'country': L[1], 'today': { 'text': L[2], 'low': L[3], 'high': L[4] }, 'tomorrow': { 'text': L[5], 'low': L[6], 'high': L[7] } }
Sign in to make a reply
请叫我林小李