Python查询天气小程序

来源:互联网 发布:2017淘宝黑科技 编辑:程序博客网 时间:2024/05/21 09:18

输入城市名,打印查询结果

#!/usr/bin/env python#encoding:utf-8#By eathingsimport urllibimport urllib2import refrom xml.dom.minidom import parseStringclass Weather:def __init__(self):self.url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName"def get_weather(self,thecityname):url = self.url + "?thecityname=" + thecitynameresult = urllib2.urlopen(url).read()dom = parseString(result)strings = dom.getElementsByTagName("string")temperature_of_today = self.getText(strings[5].childNodes)weather_of_today = self.getText(strings[6].childNodes)temperature_of_tomorrow = self.getText(strings[12].childNodes)weather_of_tomorrow = self.getText(strings[13].childNodes)weather_tips = self.getText(strings[11].childNodes)weatherStr = u"今明两天的天气状况是:\n %s %s; %s %s;\n"%\(weather_of_today,temperature_of_today,weather_of_tomorrow,temperature_of_tomorrow)weatherTips = u"友情提示:\n%s" %weather_tips print weatherStrprint weatherTipsdef getText(self,nodelist):"""获取所有的"""rc=""for node in nodelist:if node.nodeType==node.TEXT_NODE:rc=rc+node.datareturn rcweath = Weather()print "输入城市名,显示查询结果"thecityname = raw_input()weath.get_weather(thecityname)


0 0
原创粉丝点击