python實例: 天氣預(yù)報

#!/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"穿衣指數(shù):%s" %self._chy_shuoming
            print u"污染指數(shù):%s(%s)" %(self._pollution_l,self._pollution_s)
            print u"紫外線指數(shù):%s(%s)" %(self._zwx_l,self._zwx_s)
            print u"體感指數(shù):%s(%s)" %(self._ssd_l,self._ssd_s)
            print u"空調(diào)指數(shù):%s(%s)" % (self._ktk_l, self._ktk_s)
            print u"洗車指數(shù):%s(%s)" % (self._xcz_l, self._xcz_s)
            print u"感冒指數(shù):%s(%s)" %(self._gm_l,self._gm_s)
            print u"運動指數(shù):%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()

運行結(jié)果如下:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子祷肯,更是在濱河造成了極大的恐慌盲泛,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摔握,死亡現(xiàn)場離奇詭異寄狼,居然都是意外死亡,警方通過查閱死者的電腦和手機氨淌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門泊愧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人盛正,你說我怎么就攤上這事删咱。” “怎么了豪筝?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵痰滋,是天一觀的道長。 經(jīng)常有香客問我续崖,道長敲街,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任严望,我火速辦了婚禮多艇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘著蟹。我一直安慰自己墩蔓,他們只是感情好梢莽,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著奸披,像睡著了一般昏名。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上阵面,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天轻局,我揣著相機與錄音,去河邊找鬼样刷。 笑死仑扑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的置鼻。 我是一名探鬼主播镇饮,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼箕母!你這毒婦竟也來了储藐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤嘶是,失蹤者是張志新(化名)和其女友劉穎钙勃,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體聂喇,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡辖源,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了希太。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片克饶。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖跛十,靈堂內(nèi)的尸體忽然破棺而出彤路,到底是詐尸還是另有隱情,我是刑警寧澤芥映,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布洲尊,位于F島的核電站,受9級特大地震影響奈偏,放射性物質(zhì)發(fā)生泄漏坞嘀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一惊来、第九天 我趴在偏房一處隱蔽的房頂上張望丽涩。 院中可真熱鬧,春花似錦、人聲如沸矢渊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽矮男。三九已至移必,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間毡鉴,已是汗流浹背崔泵。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留猪瞬,地道東北人憎瘸。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像陈瘦,于是被迫代替她去往敵國和親幌甘。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容