我正在使用雅虎!Weather API 检索一些值。这是请求:
如何从以下获取属性和值:
<yweather:astronomy sunrise="6:20 am" sunset="4:52 pm"/>
<yweather:condition text="Partly Cloudy" code="29" temp="14" date="Fri, 09 Nov 2012 1:47 am PST"/>
我想打印类似的东西:
Sunrise: 6.20 am
Sunset: 4.52 pm
您可以使用feedparser库执行此操作:
import feedparser
feed = feedparser.parse('http://weather.yahooapis.com/forecastrss?w=2442047&u=c')
print 'Sunrise:', feed.feed.yweather_astronomy['sunrise']
print 'Sunset:', feed.feed.yweather_astronomy['sunset']
使用feedparser:
import feedparser
feed = feedparser.parse('http://weather.yahooapis.com/forecastrss?w=2442047&u=c')
sunrise = feed.items()[0][1]["yweather_astronomy"]["sunrise"]
sunset = feed.items()[0][1]["yweather_astronomy"]["sunset"]
播放结果parse以了解已解析提要的结构。