python实例: 天气预报

#!/usr/bin/env python
# -*- coding: utf-8 -*

import urllib2
import xml.etree.cElementTree as ET
from datetime import datetime

class Weather():

    def gethtml(self,url):
        self._url = url
        self._user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self._headers = {'User-Agent':self._user_agent}
        self._req = urllib2.Request(self._url,headers=self._headers)
        self._resp = urllib2.urlopen(self._req)
        self._html = self._resp.read()
        return self._html

    def getnode(self,str,xpath):
        self.node = []
        self._str = str
        self._xpath = xpath
        #tree = ET.ElementTree(html)
        #tree = ET.parse(html)
        self._tree = ET.fromstring(self._str)
        for elem in self._tree.iterfind(self._xpath):
                self.node.append(elem.text)
        return self.node[0]
    def printweather(self,city,page=0):
        self._city = str(city)
        self._city_gb = self._city.decode('utf8').encode('gb2312')
        self._page = str(page)
        self._wea_url = 'http://php.weather.sina.com.cn/xml.php?city=' + self._city_gb + '&password=DJOYnieT8234jlsK&day=' + self._page
        #print self._city
        #print self._page
        #print self._wea_url
        self._wea = self.gethtml( self._wea_url).replace("\n","")
        self._city = self.getnode(self._wea,'Weather/city')
        self._status1 = self.getnode(self._wea, 'Weather/status1')
        self._status2 = self.getnode(self._wea, 'Weather/status2')
        self._direction1 = self.getnode(self._wea, 'Weather/direction1')
        self._direction2 = self.getnode(self._wea, 'Weather/direction2')
        self._power1 = self.getnode(self._wea, 'Weather/power1')
        self._power2 = self.getnode(self._wea, 'Weather/power2')
        self._temperature1 = self.getnode(self._wea, 'Weather/temperature1')
        self._temperature2 = self.getnode(self._wea, 'Weather/temperature2')
        self._chy_shuoming = self.getnode(self._wea, 'Weather/chy_shuoming')
        self._pollution_l = self.getnode(self._wea, 'Weather/pollution_l')
        self._pollution_s = self.getnode(self._wea, 'Weather/pollution_s')
        self._zwx_l = self.getnode(self._wea, 'Weather/zwx_l')
        self._zwx_s = self.getnode(self._wea, 'Weather/zwx_s')
        self._ssd_l = self.getnode(self._wea, 'Weather/ssd_l')
        self._ssd_s = self.getnode(self._wea, 'Weather/ssd_s')
        self._ktk_l = self.getnode(self._wea, 'Weather/ktk_l')
        self._ktk_s = self.getnode(self._wea, 'Weather/ktk_s')
        self._xcz_l = self.getnode(self._wea, 'Weather/xcz_l')
        self._xcz_s = self.getnode(self._wea, 'Weather/xcz_s')
        self._gm_l = self.getnode(self._wea, 'Weather/gm_l')
        self._gm_s = self.getnode(self._wea, 'Weather/gm_s')
        self._yd_l = self.getnode(self._wea, 'Weather/yd_l')
        self._yd_s = self.getnode(self._wea, 'Weather/yd_s')
        self._savedate_weather = self.getnode(self._wea, 'Weather/savedate_weather')
        self._udatetime = self.getnode(self._wea, 'Weather/udatetime')

        #now = datetime.now()
        #self._year,self._weekth,self._week = now.isocalendar()

        print "="*100
        print u"您查询的城市是:%s" %self._city
        if self._page in ['','0','1','2','3','4']:
            print self._savedate_weather
            print "-" * 50
            print u"天气更新时间:%s" %self._udatetime
            print u"白天:%s %s(%s级) 最高温度%s℃" %(self._status1, self._direction1,self._power1,self._temperature1)
            print u"夜里:%s %s(%s级) 最低温度%s℃" %(self._status2, self._direction2,self._power2,self._temperature2)
            print "-"*50
            print u"穿衣指数:%s" %self._chy_shuoming
            print u"污染指数:%s(%s)" %(self._pollution_l,self._pollution_s)
            print u"紫外线指数:%s(%s)" %(self._zwx_l,self._zwx_s)
            print u"体感指数:%s(%s)" %(self._ssd_l,self._ssd_s)
            print u"空调指数:%s(%s)" % (self._ktk_l, self._ktk_s)
            print u"洗车指数:%s(%s)" % (self._xcz_l, self._xcz_s)
            print u"感冒指数:%s(%s)" %(self._gm_l,self._gm_s)
            print u"运动指数:%s(%s)" %(self._yd_l,self._yd_s)
            print "=" * 100
        else:
            print "Your input is not right!"
            print "=" * 100
def main():
    while True:
        myinput1 = raw_input("Please input the city:")
        myinput2 = raw_input("Please input (0 1 2 3 4):")
        try:
            if myinput1 =='exit' or myinput2 == 'exit':
                print "!!!!"
                break
            else:
                #myinput = raw_input("Please input (0 1 2 3 4):")
                pass
        except (ValueError, TypeError):
            print "your input is not right!"

        myweather = Weather()
        myweather.printweather(myinput1,myinput2)

if __name__ == '__main__':
    main()

运行结果如下:

python实例: 天气预报_第1张图片

你可能感兴趣的:(python实例: 天气预报)