本文主要是介绍Python查询天气小程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
输入城市名,打印查询结果
#!/usr/bin/env python
#encoding:utf-8
#By eathingsimport urllib
import urllib2
import re
from 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)
这篇关于Python查询天气小程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!