python 读取yahoo 天气api 获取北京天气情况
代码如下:
#!/usr/bin/env python # coding=utf-8 import urllib2 import xml.dom.minidom try: url="http://weather.yahooapis.com/forecastrss?u=c&w=2151330" request = urllib2.Request(url) response = urllib2.urlopen(request) print "Geting data from yahooapis...." data = response.read() #print data dom = xml.dom.minidom.parseString(data) root = dom.documentElement location = root.getElementsByTagName(‘yweather:location‘) city = location[0].getAttribute("city") weather_conditions=root.getElementsByTagName(‘yweather:condition‘) weather = weather_conditions[0].getAttribute(‘text‘) temp = weather_conditions[0].getAttribute(‘temp‘) print "City:",city print "weather:",weather print "temp:",temp except Exception: print("there is something wrong")
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。