1. 程式人生 > >Python中使用SAX解析XML及例項

Python中使用SAX解析XML及例項

  1. 在學習廖雪峰老師的Python教程時,學到了難以理解的關於SAX解析XML這一節。本文將從此節出發,逐句的分析此節的作業。其中作業來源於網友評論。
  2. SAX解析XML速度快、佔用記憶體小。我們只需要關注三個事件:start_element、end_element、char_data。如:當SAX在解析一個節點時<li><a href="/python">Python</a></li> 會產生三個事件:
    2.1 start_element事件,分別讀取<li><a href="/python">
    2.2 end_element
    事件,分別讀取</a></li>
    2.3 char_data事件、讀取Python
  3. 補充二維字典知識:
    3.1 定義二維字典 :dict_2d = {'a': {'a': 1, 'b': 3}, 'b': {'a': 6}}
    3.2 訪問二維字典:dict_2d['a']['a'] ,結果明顯是:1
    3.3 新增二維字典中屬性時可以用一個函式來完成:
#二維字典的新增函式
def addtwodimdict(thedict, key_a, key_b, val):
  if key_a in adic:
    thedict[key_a].update({key_b: val})
  else
: thedict.update({key_a:{key_b: val}})


4. 例項:請利用SAX編寫程式解析Yahoo的XML格式的天氣預報,獲取當天和第二天的天氣:

#!/uer/bin/env python
#-*- coding:utf-8 -*-
#使用SAX解析XML
#查詢yahoo天氣的今天和明天天氣
#題目及程式碼來源:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432002075057b594f70ecb58445da6ef6071aca880af000
#宣告
from xml.parsers.expat import
ParserCreate #定義天氣字典、天數 weather_dict = {} which_day = 0 #定義解析類 #包括三個主要函式:start_element(),end_element(),char_data() class WeatherSaxHandler(object): #定義start_element函式 def start_element(self,name,attrs): global weather_dict,which_day #判斷並獲取XML文件中地理位置資訊 if name == 'yweather:location': #將本行XML程式碼中'city'屬性值賦予字典weather_dict中的'city' weather_dict['city']=attrs['city'] weather_dict['country']=attrs['country']#執行結束後此時,weather_dict={'city':'Beijing','country'='China'} #同理獲取天氣預測資訊 if name == 'yweather:forecast': which_day +=1 #第一天天氣,獲取氣溫、天氣 if which_day == 1: weather ={'text':attrs['text'], 'low':int(attrs['low']), 'high':int(attrs['high']) } weather_dict['today']=weather#此時weather_dict出現二維字典 #weather_dict={'city': 'Beijing', 'country': 'China', 'today': {'text': 'Partly Cloudy', 'low': 20, 'high': 33}} #第二天相關資訊 elif which_day==2: weather={ 'text':attrs['text'], 'low':int(attrs['low']), 'high':int(attrs['high']) } weather_dict['tomorrow']=weather #weather_dict={'city': 'Beijing', 'country': 'China', 'today': {'text': 'Partly Cloudy', 'low': 20, 'high': 33}, 'tomorrow': {'text': 'Sunny', 'low': 21, 'high': 34}} #end_element函式 def end_element(self,name): pass #char_data函式 def char_data(self,text): pass def parse_weather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) return weather_dict #XML文件,輸出結果的資料來源 #將XML文件賦值給data data = r'''<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <channel> <title>Yahoo! Weather - Beijing, CN</title> <lastBuildDate>Wed, 27 May 2015 11:00 am CST</lastBuildDate> <yweather:location city="Beijing" region="" country="China"/> <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/> <yweather:wind chill="28" direction="180" speed="14.48" /> <yweather:atmosphere humidity="53" visibility="2.61" pressure="1006.1" rising="0" /> <yweather:astronomy sunrise="4:51 am" sunset="7:32 pm"/> <item> <geo:lat>39.91</geo:lat> <geo:long>116.39</geo:long> <pubDate>Wed, 27 May 2015 11:00 am CST</pubDate> <yweather:condition text="Haze" code="21" temp="28" date="Wed, 27 May 2015 11:00 am CST" /> <yweather:forecast day="Wed" date="27 May 2015" low="20" high="33" text="Partly Cloudy" code="30" /> <yweather:forecast day="Thu" date="28 May 2015" low="21" high="34" text="Sunny" code="32" /> <yweather:forecast day="Fri" date="29 May 2015" low="18" high="25" text="AM Showers" code="39" /> <yweather:forecast day="Sat" date="30 May 2015" low="18" high="32" text="Sunny" code="32" /> <yweather:forecast day="Sun" date="31 May 2015" low="20" high="37" text="Sunny" code="32" /> </item> </channel> </rss> ''' #例項化類 weather = parse_weather(data) #檢查條件是否為True assert weather['city'] == 'Beijing', weather['city'] assert weather['country'] == 'China', weather['country'] assert weather['today']['text'] == 'Partly Cloudy', weather['today']['text'] assert weather['today']['low'] == 20, weather['today']['low'] assert weather['today']['high'] == 33, weather['today']['high'] assert weather['tomorrow']['text'] == 'Sunny', weather['tomorrow']['text'] assert weather['tomorrow']['low'] == 21, weather['tomorrow']['low'] assert weather['tomorrow']['high'] == 34, weather['tomorrow']['high'] #列印到螢幕 print('Weather:', str(weather))


5. 通過本節的學習,對Python中SAX對XML的解析有了簡單的瞭解,為以後爬蟲學習做準備。