分享

Python查询天气小程序

 梨惠子 2014-04-24

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

  1. #!/usr/bin/env python  
  2. #encoding:utf-8  
  3. #By eathings  
  4.   
  5. import urllib  
  6. import urllib2  
  7. import re  
  8. from xml.dom.minidom import parseString  
  9.   
  10. class Weather:  
  11.     def __init__(self):  
  12.         self.url = "http://www./webservices/weatherwebservice.asmx/getWeatherbyCityName"  
  13.   
  14.     def get_weather(self,thecityname):  
  15.         url = self.url + "?thecityname=" + thecityname  
  16.         result = urllib2.urlopen(url).read()  
  17.         dom = parseString(result)  
  18.         strings = dom.getElementsByTagName("string")  
  19.         temperature_of_today = self.getText(strings[5].childNodes)  
  20.         weather_of_today = self.getText(strings[6].childNodes)  
  21.         temperature_of_tomorrow = self.getText(strings[12].childNodes)  
  22.         weather_of_tomorrow = self.getText(strings[13].childNodes)  
  23.         weather_tips = self.getText(strings[11].childNodes)  
  24.         weatherStr = u"今明两天的天气状况是:\n %s %s; %s %s;\n"%\  
  25.                 (weather_of_today,temperature_of_today,  
  26.                 weather_of_tomorrow,temperature_of_tomorrow)  
  27.         weatherTips = u"友情提示:\n%s" %weather_tips   
  28.         print weatherStr  
  29.         print weatherTips  
  30.   
  31.   
  32.     def getText(self,nodelist):  
  33.         """ 
  34.         获取所有的 
  35.         """  
  36.         rc=""  
  37.         for node in nodelist:  
  38.             if node.nodeType==node.TEXT_NODE:  
  39.                 rc=rc+node.data  
  40.         return rc  
  41.   
  42.   
  43. weath = Weather()  
  44. print "输入城市名,显示查询结果"  
  45. thecityname = raw_input()  
  46. weath.get_weather(thecityname) 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约