求助,新浪天气API返回的xml,不知道该怎么解析了。
本帖最后由 liyan5953 于 2014-04-24 23:32 编辑新浪天气API查询,返回的XML是这样样子的。
该怎么解析啊,哪位大神教教我。
<?xml version="1.0" encoding="UTF-8"?> <!-- published at 2014-04-24 23:18:22 --> <Profiles> <Weather> <city>广州</city> <status1>阴</status1> <status2>阴</status2> <figure1>yin</figure1> <figure2>yin</figure2> <direction1>无持续风向</direction1> <direction2>无持续风向</direction2> <power1>≤3</power1> <power2>≤3</power2> <temperature1>27</temperature1> <temperature2>22</temperature2> <ssd>7</ssd> <tgd1>24</tgd1> <tgd2>24</tgd2> <zwx>1</zwx> <ktk>4</ktk> <pollution>3</pollution> <xcz>4</xcz> <zho></zho> <diy></diy> <fas></fas> <chy>1</chy> <zho_shuoming>暂无</zho_shuoming> <diy_shuoming>暂无</diy_shuoming> <fas_shuoming>暂无</fas_shuoming> <chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming> <pollution_l>轻度</pollution_l> <zwx_l>最弱</zwx_l> <ssd_l>偏热</ssd_l> <fas_l>暂无</fas_l> <zho_l>暂无</zho_l> <chy_l>薄短袖类</chy_l> <ktk_l>不需要开启</ktk_l> <xcz_l>不太适宜</xcz_l> <diy_l>暂无</diy_l> <pollution_s>对空气污染物扩散无明显影响</pollution_s> <zwx_s>紫外线最弱</zwx_s> <ssd_s>天气偏热,适当调整穿衣后,仍可达到比较舒适的程度。</ssd_s> <ktk_s>不需要开启空调</ktk_s> <xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车</xcz_s> <gm>1</gm> <gm_l>低发期</gm_l> <gm_s>天气舒适,不易发生感冒;</gm_s> <yd>3</yd> <yd_l>适宜</yd_l> <yd_s>天气较暖,比较适宜户外运动;</yd_s> <savedate_weather>2014-04-24</savedate_weather> <savedate_life>2014-04-24</savedate_life> <savedate_zhishu>2014-04-24</savedate_zhishu> </Weather> </Profiles> 用 xml.etree 或 mindom; LZ可能不懂Python XML API,更可能看不懂这个XML。。。$ xmllint --format tmp.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- published at 2014-04-24 23:18:22 -->
<Profiles>
<Weather>
<city>广州</city>
<status1>阴</status1>
<status2>阴</status2>
<figure1>yin</figure1>
<figure2>yin</figure2>
<direction1>无持续风向</direction1>
<direction2>无持续风向</direction2>
<power1>≤3</power1>
<power2>≤3</power2>
<temperature1>27</temperature1>
<temperature2>22</temperature2>
<ssd>7</ssd>
<tgd1>24</tgd1>
<tgd2>24</tgd2>
<zwx>1</zwx>
<ktk>4</ktk>
<pollution>3</pollution>
<xcz>4</xcz>
<zho/>
<diy/>
<fas/>
<chy>1</chy>
<zho_shuoming>暂无</zho_shuoming>
<diy_shuoming>暂无</diy_shuoming>
<fas_shuoming>暂无</fas_shuoming>
<chy_shuoming>短袖衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫</chy_shuoming>
<pollution_l>轻度</pollution_l>
<zwx_l>最弱</zwx_l>
<ssd_l>偏热</ssd_l>
<fas_l>暂无</fas_l>
<zho_l>暂无</zho_l>
<chy_l>薄短袖类</chy_l>
<ktk_l>不需要开启</ktk_l>
<xcz_l>不太适宜</xcz_l>
<diy_l>暂无</diy_l>
<pollution_s>对空气污染物扩散无明显影响</pollution_s>
<zwx_s>紫外线最弱</zwx_s>
<ssd_s>天气偏热,适当调整穿衣后,仍可达到比较舒适的程度。</ssd_s>
<ktk_s>不需要开启空调</ktk_s>
<xcz_s>洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车</xcz_s>
<gm>1</gm>
<gm_l>低发期</gm_l>
<gm_s>天气舒适,不易发生感冒;</gm_s>
<yd>3</yd>
<yd_l>适宜</yd_l>
<yd_s>天气较暖,比较适宜户外运动;</yd_s>
<savedate_weather>2014-04-24</savedate_weather>
<savedate_life>2014-04-24</savedate_life>
<savedate_zhishu>2014-04-24</savedate_zhishu>
</Weather>
</Profiles>
不好意思,这个问题是我白痴了。
一直在在用ET.fromstring读取xml,然后用xml.find('city').text需找节点,一直报错。
其实在需要节点的时候很简单的加上节点结构就行了。xml.find('Weather/city').text。就这样就可以了。。。。。。
页:
[1]