获取城市天气的Python脚本
发布时间:2020-12-17 17:09:52 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 原文? http://xianglong.me/article/get-city-weather-using-python-script/ #!/usr/bin/env python# -*- coding: utf-8 -*-# import requestsimport
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
原文?
http://xianglong.me/article/get-city-weather-using-python-script/
#!/usr/bin/env python # -*- coding: utf-8 -*- # import requests import urllib import json import sys from datetime import datetime,timedelta base_url = "http://api.worldweatheronline.com/free/v2/weather.ashx" codes = {'113': 'iconSunny','116': 'iconPartlyCloudy',"119": 'iconCloudy','122': 'iconVeryCloudy','143': 'iconFog','176': 'iconLightShowers','179': 'iconLightSleetShowers','182': 'iconLightSleet','185': 'iconLightSleet','200': 'iconThunderyShowers','227': 'iconLightSnow','230': 'iconHeavySnow','248': 'iconFog','260': 'iconFog','263': 'iconLightShowers','266': 'iconLightRain','281': 'iconLightSleet','284': 'iconLightSleet','293': 'iconLightRain','296': 'iconLightRain','299': 'iconHeavyShowers','302': 'iconHeavyRain','305': 'iconHeavyShowers','308': 'iconHeavyRain','311': 'iconLightSleet','314': 'iconLightSleet','317': 'iconLightSleet','320': 'iconLightSnow','323': 'iconLightSnowShowers','326': 'iconLightSnowShowers','329': 'iconHeavySnow','332': 'iconHeavySnow','335': 'iconHeavySnowShowers','338': 'iconHeavySnow','350': 'iconLightSleet','353': 'iconLightShowers','356': 'iconHeavyShowers','359': 'iconHeavyRain','362': 'iconLightSleetShowers','365': 'iconLightSleetShowers','368': 'iconLightSnowShowers','371': 'iconHeavySnowShowers','374': 'iconLightSleetShowers','377': 'iconLightSleet','386': 'iconThunderyShowers','389': 'iconThunderyHeavyRain','392': 'iconThunderySnowShowers','395': 'iconHeavySnowShowers' } icons = { 'iconSunny': [ ' 33[38;5;226m / 33[0m',' 33[38;5;226m .-. 33[0m',' 33[38;5;226m ― ( ) ― 33[0m',' 33[38;5;226m `-’ 33[0m',' 33[38;5;226m / 33[0m'],'iconPartlyCloudy': [ " 33[38;5;226m / 33[0m "," 33[38;5;226m _ /"" 33[38;5;250m.-. 33[0m"," 33[38;5;226m _ 33[38;5;250m( ). 33[0m"," 33[38;5;226m / 33[38;5;250m(___(__) 33[0m"," "],'iconCloudy': [ " "," 33[38;5;250m .--. 33[0m"," 33[38;5;250m .-( ). 33[0m"," 33[38;5;250m (___.__)__) 33[0m",'iconVeryCloudy': [ " "," 33[38;5;240;1m .--. 33[0m"," 33[38;5;240;1m .-( ). 33[0m"," 33[38;5;240;1m (___.__)__) 33[0m",'iconLightShowers': [ " 33[38;5;226m _`/"" 33[38;5;250m.-. 33[0m"," 33[38;5;226m,_ 33[38;5;250m( ). 33[0m"," 33[38;5;111m ‘ ‘ ‘ ‘ 33[0m"," 33[38;5;111m ‘ ‘ ‘ ‘ 33[0m"],'iconHeavyShowers': [ " 33[38;5;226m _`/"" 33[38;5;240;1m.-. 33[0m",_ 33[38;5;240;1m( ). 33[0m"," 33[38;5;226m / 33[38;5;240;1m(___(__) 33[0m"," 33[38;5;21;1m ?‘?‘?‘?‘ 33[0m"," 33[38;5;21;1m ?’?’?’?’ 33[0m"],'iconLightSnowShowers': [ " 33[38;5;226m _`/"" 33[38;5;250m.-. 33[0m"," 33[38;5;255m * * * 33[0m"," 33[38;5;255m * * * 33[0m"],'iconHeavySnowShowers': [ " 33[38;5;226m _`/"" 33[38;5;240;1m.-. 33[0m"," 33[38;5;255;1m * * * * 33[0m"," 33[38;5;255;1m * * * * 33[0m"],'iconLightSleetShowers': [ " 33[38;5;226m _`/"" 33[38;5;250m.-. 33[0m"," 33[38;5;111m ‘ 33[38;5;255m* 33[38;5;111m ‘ 33[38;5;255m* 33[0m"," 33[38;5;255m * 33[38;5;111m ‘ 33[38;5;255m* 33[38;5;111m ‘ 33[0m"],'iconThunderyShowers': [ " 33[38;5;226m _`/"" 33[38;5;250m.-. 33[0m"," 33[38;5;228;5m ? 33[38;5;111;25m‘ ‘ 33[38;5;228;5m? 33[38;5;111;25m‘ ‘ 33[0m",'iconThunderyHeavyRain': [ " 33[38;5;240;1m .-. 33[0m"," 33[38;5;240;1m ( ). 33[0m"," 33[38;5;240;1m (___(__) 33[0m"," 33[38;5;21;1m ?‘ 33[38;5;228;5m? 33[38;5;21;25m‘? 33[38;5;228;5m? 33[38;5;21;25m?‘ 33[0m"," 33[38;5;21;1m ?’?’ 33[38;5;228;5m? 33[38;5;21;25m’?’ 33[0m"],'iconThunderySnowShowers': [ " 33[38;5;226m _`/"" 33[38;5;250m.-. 33[0m"," 33[38;5;255m * 33[38;5;228;5m? 33[38;5;255;25m * 33[38;5;228;5m? 33[38;5;255;25m * 33[0m",'iconLightRain': [ " 33[38;5;250m .-. 33[0m"," 33[38;5;250m ( ). 33[0m"," 33[38;5;250m (___(__) 33[0m"," 33[38;5;111m ‘ ‘ ‘ ‘ 33[0m"," 33[38;5;111m ‘ ‘ ‘ ‘ 33[0m"],'iconHeavyRain': [ " 33[38;5;240;1m .-. 33[0m"," 33[38;5;21;1m ?‘?‘?‘?‘ 33[0m"," 33[38;5;21;1m ?’?’?’?’ 33[0m"],'iconLightSnow': [ " 33[38;5;250m .-. 33[0m"," 33[38;5;255m * * * 33[0m"," 33[38;5;255m * * * 33[0m"],'iconHeavySnow': [ " 33[38;5;240;1m .-. 33[0m"," 33[38;5;255;1m * * * * 33[0m"," 33[38;5;255;1m * * * * 33[0m"],'iconLightSleet': [ " 33[38;5;250m .-. 33[0m"," 33[38;5;111m ‘ 33[38;5;255m* 33[38;5;111m ‘ 33[38;5;255m* 33[0m"," 33[38;5;255m * 33[38;5;111m ‘ 33[38;5;255m* 33[38;5;111m ‘ 33[0m"],'iconFog': [ " "," 33[38;5;251m _ - _ - _ - 33[0m"," 33[38;5;251m _ - _ - _ 33[0m",'iconUnknown': [ " .-. "," __) "," ( "," `-’ "," ? "] } windDir = { "N": " 33[1m↓ 33[0m","NNE": " 33[1m↓ 33[0m","NE": " 33[1m↙ 33[0m","ENE": " 33[1m↙ 33[0m","E": " 33[1m← 33[0m","ESE": " 33[1m← 33[0m","SE": " 33[1m↖ 33[0m","SSE": " 33[1m↖ 33[0m","S": " 33[1m↑ 33[0m","SSW": " 33[1m↑ 33[0m","SW": " 33[1m↗ 33[0m","WSW": " 33[1m↗ 33[0m","W": " 33[1m→ 33[0m","WNW": " 33[1m→ 33[0m","NW": " 33[1m↘ 33[0m","NNW": " 33[1m↘ 33[0m",} class Query(object): def __init__(self,day,city): self.day = day self.time = [3,4,5,6] self.weatherCode = '' self.weather = '' self.date = '' self.hourly = '' self.tempC = 0 self.winddir16Point = '' self.windspeedKmph = 0 self.humidity = 0 self.chanceofwater = 0 self.city = city def query(self): # 使用resquests # p={"q":"%s"%self.city,"num_of_days":3,"format":"json","key":"55f1fdd05fba23be0a18043d0a017","lang":"zh"} # response=requests.get(base_url,params=p) # json_string = response.text #使用urllib url = base_url + "?key=55f1fdd05fba23be0a18043d0a017&q=%s#_of_days=3&format=json?=zh" % self.city response = urllib.urlopen(url) json_string = response.read() parsed_json = json.loads(json_string) data = parsed_json['data'] # 获取所有数据 try: self.weather = data['weather'][self.day] # 获取天气预报,[]内0代表当天,1代表明天,以此类推。 except KeyError: print " 33[1;31;49m" + "请输入正确的城市或地区!" + " 33[0m" sys.exit() self.date = self.weather['date'] def detail(self,time): self.hourly = self.weather['hourly'][time] # 获取小时数据,time:100-1500 self.weatherCode = self.hourly['weatherCode'] self.tempC = self.hourly['tempC'] self.winddir16Point = self.hourly['winddir16Point'] self.windspeedKmph = self.hourly['windspeedKmph'] self.chanceofrain = self.hourly['chanceofrain'] self.chanceofsnow = self.hourly['chanceofsnow'] self.humidity = self.hourly['humidity'] self.chanceofwater = int(self.chanceofrain) if int(self.chanceofrain) > int(self.chanceofsnow) else int(self.chanceofsnow) def printSingle(self): l1 = l2 = l3 = l4 = l5 = '' for time in self.time: self.detail(time) l1 += '│' + icons[codes[self.weatherCode]][0] + self.hourly['lang_zh'][0]['value'].encode("utf-8") + 'tt' if len(self.hourly['lang_zh'][0]['value'].encode("utf-8")) <= 12 else '│' + icons[codes[self.weatherCode]][0] + self.hourly['lang_zh'][0]['value'].encode("utf-8") + 't' l2 += '│' + icons[codes[self.weatherCode]][1] + temp_color(self.tempC) + "°C"+'tt' l3 += '│' + icons[codes[self.weatherCode]][2] + windDir[self.winddir16Point]+" "+ wind_color(self.windspeedKmph) + "km/h" + 'tt' l4 += '│' + icons[codes[self.weatherCode]][3] + "降水概率:" + str(self.chanceofwater) + "%" + 't' l5 += '│' + icons[codes[self.weatherCode]][4] + "湿度:" + str(self.humidity) + "%" + 'tt' print l1+"│" print l2+"│" print l3+"│" print l4+"│" print l5+"│" def printDay(self,delta): date_time = datetime.strftime(datetime.today() + timedelta(days=delta),"%Y-%m-%d") line1 = " ┌─────────────┐ " line2 = "┌───────────────────────────────┬──────────────────────────%s───────────────────────────┬───────────────────────────────┐" % date_time line3 = "│ Morning │ Noon └──────┬──────┘ Evening │ Night │" line4 = "├───────────────────────────────┼───────────────────────────────┼───────────────────────────────┼───────────────────────────────┤" endline= "└───────────────────────────────┴───────────────────────────────┴───────────────────────────────┴───────────────────────────────┘" print line1 print line2 print line3 print line4 self.printSingle() print endline def temp_color(temp): if temp >= 35 or temp <= -10: color = " 33[1;31;49m" + str(temp) + " 33[0m" elif (temp >= 25 and temp <35): color = " 33[1;33;49m" + str(temp) + " 33[0m" elif temp > 10 and temp < 25: color = " 33[1;32;49m" + str(temp) + " 33[0m" elif temp >-10 and temp <= 10: color = " 33[1;34;49m" + str(temp) + " 33[0m" return color def wind_color(windspeed): if windspeed <= 5: color = " 33[1;32;49m" + str(windspeed) + " 33[0m" elif windspeed > 5 and windspeed <=10: color = " 33[1;33;49m" + str(windspeed) + " 33[0m" else: color = " 33[1;34;49m" + str(windspeed) + " 33[0m" return color def main(): try: city = sys.argv[1] except IndexError: print " 33[1;31;49m" + "请输入您要查询的城市或地区:" + " 33[0m" city = raw_input() if city == '': sys.exit() day = [0,1,2] for i in day: query = Query(i,city) query.query() query.printDay(i) if __name__ == "__main__": main() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |