#!-*- coding:utf-8 -*-
# @author: dwcai
# @contact:
# @software: PyCharm
# @file: weather_query.py
# @time: 2019-12-05 22:59
# @desc: 查询天气
# --------------------- sep ---------------------
import requests
import json
class QueryWeather:
def __init__(self, query_city, query_days):
self.__appid = 'YOUR APPID' # 修改为你的用户 ID
self.__appsecret = 'YOUR APPSECRET' # 修改为你的用户 密钥
self.param_dict = {'day': '日期:\t', 'week': '星期:\t', 'wea': '天气:\t', 'tem': '温度:\t', 'tem1': '高温:\t', 'tem2': '低温:\t', 'win': '风向:\t', 'win_speed': '风速:\t', 'air_level': '空气:\t', 'air_tips': '建议:\t'}
self.city = query_city
self.query_days = query_days
self.url = f"https://www. \
f"{self.city}&appid={self.__appid}&appsecret={self.__appsecret}"
self.result = self.get()
def get(self):
ret = requests.get(self.url)
ret.encoding = 'utf-8'
result = json.loads(ret.text)
return result
def show(self):
self.__show_info()
for i in range(self.query_days):
for key, val in self.param_dict.items():
if (i > 0) and (key is 'air_level'):
pass
elif (i > 0) and (key is 'air_tips'):
pass
else:
weather_info = str(val) + str(self.result['data'][i][key])
print(weather_info)
print("-"*40)
def __show_info(self):
print("国家: %s\n城市: %s\n更新时间: %s" %
(self.result['country'], self.result['city'], self.result['update_time']))
print(self.result['city'] + "最近%d天的天气情况如下: " % self.query_days)
print("-"*40)
if __name__ == "__main__":
city = '北京'
days = 3
query = QueryWeather(city, days)
query.show()