python学习之常用的内置类(4):python中的XML
来源:互联网 发布:tensorflow vgg 编辑:程序博客网 时间:2024/05/22 06:31
XML,一般使用Sax进行分析,下面使用sax来进行分析yahoo天气网页的xml,获取需要的今天和明天的信息.并输出.
from xml.parsers.expat import ParserCreateclass WeatherSaxHandler(object): def __init__(self): self._days = 0 self._weather = {} def start_element(self,name,attrs): print('sax:start_element:%s attrs:'%name,attrs) if('city' in attrs or 'country' in attrs):self._weather['city'],self._weather['country'] = attrs['city'],attrs['country'] if(('day' in attrs) and self._days == 0): self._days += 1 self._weather['today'] = {'text':attrs['text'],'low':int(attrs['low']),'high':int(attrs['high'])} elif(('day' in attrs) and self._days == 1): self._days += 1 self._weather['tomorrow'] ={'text':attrs['text'],'low':int(attrs['low']),'high':int(attrs['high'])} def end_element(self,name): print('sax:end_element: %s' % name) def char_data(self,text): print('sax:char_data: %s' % text)def parse_weather(xml): weatherparser = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = weatherparser.start_element parser.EndElementHandler = weatherparser.end_element parser.CharacterDataHandler = weatherparser.char_data parser.Parse(xml) return weatherparser._weatherdata = 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)assert weather['city'] == 'Beijing', weather['city']assert weather['country'] == 'China', weather['country']assert weather['today']['text'] == 'Partly Cloudy', weather['today']['text']print('today;low:',weather['today']['low'] == 20,weather['today']['low']) 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))
0 0
- python学习之常用的内置类(4):python中的XML
- Python学习笔记之常用的内置函数
- Python学习之常用内置模块:urlib
- 学习python 之 内置的函数
- [python] 类常用的内置方法
- [python] 类常用的内置方法
- [python] 类常用的内置方法
- [python] 类常用的内置方法
- python类常用的内置方法
- [python] 类常用的内置方法
- python 常用的内置函数
- Python-常用的内置函数
- Python常用的内置函数
- python学习之内置数据类型
- python 常用的Python内置函数
- [python]python内置容器的学习
- [python]python常用内置函数
- Python常用内置函数
- /* 判断一个对象是不是数组*/
- hadoop 2.7.2开发环境配置
- hdu——1106排序(重定向)
- 绘制模拟Android钟 ####时钟表盘
- Linux命令对应的全称解释
- python学习之常用的内置类(4):python中的XML
- Android 防内存泄露handler
- Leetcode #12 & #13 Integer to Roman & Roman to Integer 解题小节
- js惰性载入-性能-滚轮事件
- git创建项目并上传代码
- Spring AOP原理解释
- Blcoking I/O & NonBlocking I/O
- la4327 优先队列优化dp
- synchronized 方法与锁对象