Python中使用SAX解析XML及实例

来源:互联网 发布:js文件如何缩小 编辑:程序博客网 时间:2024/06/06 17:21
  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):        passdef 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文档赋值给datadata = 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)#检查条件是否为Trueassert 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的解析有了简单的了解,为以后爬虫学习做准备。