close
之前有搞一個星座的parse跟氣象的parse,結果如右邊plugin呈現。
一度的把訊息寄到bbs的個人版上去,當作每日一文摘要。

今天剛好看到這個,現成的api service。
秉持著不重複造輪的精神。

修改一下放入每日一跑的crontab 中。
這樣就獲得了每天天氣預報了。

API · zonble/twweather Wiki
https://github.com/zonble/twweather/wiki/api

用法
getweather(id)

print getweather('12')
print getweather('11')

嘉義 天氣
==========
今日白天
時間 2012-04-14 12:00:00/2012-04-14 18:00:00
天氣狀況 晴時多雲
溫度 26 ~ 30 度
降雨機率 10 %
----------
今晚明晨
時間 2012-04-14 18:00:00/2012-04-15 06:00:00
天氣狀況 多雲
溫度 21 ~ 26 度
降雨機率 10 %
----------
明日白天
時間 2012-04-15 06:00:00/2012-04-15 18:00:00
天氣狀況 多雲
溫度 21 ~ 30 度
降雨機率 10 %
----------

雲林 天氣
==========
今日白天
時間 2012-04-14 12:00:00/2012-04-14 18:00:00
天氣狀況 晴時多雲
溫度 26 ~ 30 度
降雨機率 10 %
----------
今晚明晨
時間 2012-04-14 18:00:00/2012-04-15 06:00:00
天氣狀況 多雲
溫度 21 ~ 26 度
降雨機率 20 %
----------
明日白天
時間 2012-04-15 06:00:00/2012-04-15 18:00:00
天氣狀況 多雲
溫度 21 ~ 29 度
降雨機率 10 %
----------


"""
http://twweatherapi.appspot.com/forecast?location=12&output=json
"""
import urllib2
import pprint

def getweather(id):
result=''
url='http://twweatherapi.appspot.com/forecast?location='+id+'&output=json'
req=urllib2.urlopen(url)
#print req.read()
chiadict=eval(req.read())
#print chiadict
#print chiadict['result']
#pprint.pprint(chiadict['result'])
#print '氣象預報'
#print "="*20
result=result+chiadict['result']['locationName']+' 天氣\n'
result+="="*10
result+="\n"
for d in chiadict['result']['items']:
result+=''+d['title']+'\n'
result+='時間 '+d['time']+'\n'
result+='天氣狀況 '+d['description']+'\n'
result+='溫度 '+d['temperature']+' 度'+'\n'
result+='降雨機率 '+d['rain']+' %\n'
result+='-'*10
result+="\n"
return result
arrow
arrow
    文章標籤
    python api 天氣 urllib2
    全站熱搜

    peicheng 發表在 痞客邦 留言(0) 人氣()