输入城市名,打印查询结果
#!/usr/bin/env python
#encoding:utf-8
#By eathings
import urllib
import urllib2
import re
from xml.dom.minidom import parseString
class Weather:
def __init__(self):
self.url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName"
def get_weather(self,thecityname):
url = self.url + "?thecityname=" + thecityname
result = 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 weatherStr
print weatherTips
def getText(self,nodelist):
"""
获取所有的
"""
rc=""
for node in nodelist:
if node.nodeType==node.TEXT_NODE:
rc=rc+node.data
return rc
weath = Weather()
print "输入城市名,显示查询结果"
thecityname = raw_input()
weath.get_weather(thecityname)