Python中使用SAX解析XML及实例

  1. 在学习廖雪峰老师的Python教程时,学到了难以理解的关于SAX解析XML这一节。本文将从此节出发,逐句的分析此节的作业。其中作业来源于网友评论。
  2. SAX解析XML速度快、占用内存小。我们只需要关注三个事件:start_element、end_element、char_data。如:当SAX在解析一个节点时
  3. Python
  4. 会产生三个事件:
    2.1 start_element事件,分别读取

  5. 2.2 end_element事件,分别读取

  6. 2.3 char_data事件、读取Python
  7. 补充二维字典知识:
    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'''

    
        Yahoo! Weather - Beijing, CN
        Wed, 27 May 2015 11:00 am CST
        
        
        
        
        
        
            39.91
            116.39
            Wed, 27 May 2015 11:00 am CST
            
            
            
            
            
            
        
    

'''
#实例化类
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的解析有了简单的了解,为以后爬虫学习做准备。

你可能感兴趣的:(python入门)