import requestsfrom aip import AipSpeechimport os""" 你的 APPID AK SK """ # 百度人工智慧的id keyAPP_ID = ' 'API_KEY = ' 'SECRET_KEY = ' 'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)print('歡迎使用查詢未來五天天氣(必須聯網)')while True: city=input('請輸入地區:') data = input('請輸入查詢的某天(當天/明天/後天/大後天/第五天):') if not city: break #獲取天氣網址並異常處理 try: req=requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s'%city) #req.encoding = 'utf8' #print(req.text) except: print("查詢失敗,請檢查網路是否連線") input("請回車退出") break # 建立字典 dic_city = req.json() #print(dic_city) #獲取資料 city_data = dic_city.get('data') def speak(t=''): result = client.synthesis(t, 'zh', 0.5, {'vol': 5, 'per': 4}) if not isinstance(result, dict): with open('天氣.mp3', 'wb') as f: f.write(result) #建立輸出函式 def shuchu(): print('日期:', city_forecast.get('date')) print('高溫:', city_forecast.get('high')) print('低溫:', city_forecast.get('low')) print('天氣:', city_forecast.get('type')) print('風力:', city_forecast.get('fengli')) print('風向:', city_forecast.get('fengxiang')) print('注意:', city_data.get('ganmao')) speak(city+'天氣'+city_forecast.get('date')\ +city_forecast.get('high')+city_forecast.get('low')\ +city_forecast.get('type')+city_forecast.get('fengli')\ +city_forecast.get('fengxiang')+city_data.get('ganmao')) os.system('天氣.mp3') #開啟音訊檔案播放 if city_data: if data == str('當天'): city_forecast = city_data['forecast'][0] shuchu() if data == str('明天'): city_forecast = city_data['forecast'][1] shuchu() if data == str('後天'): city_forecast = city_data['forecast'][2] shuchu() if data == str('大後天'): city_forecast = city_data['forecast'][3] shuchu() if data == str('第五天'): city_forecast = city_data['forecast'][4] shuchu() else: print('未獲得') breakinput('請按回車鍵退出')
import requestsfrom aip import AipSpeechimport os""" 你的 APPID AK SK """ # 百度人工智慧的id keyAPP_ID = ' 'API_KEY = ' 'SECRET_KEY = ' 'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)print('歡迎使用查詢未來五天天氣(必須聯網)')while True: city=input('請輸入地區:') data = input('請輸入查詢的某天(當天/明天/後天/大後天/第五天):') if not city: break #獲取天氣網址並異常處理 try: req=requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s'%city) #req.encoding = 'utf8' #print(req.text) except: print("查詢失敗,請檢查網路是否連線") input("請回車退出") break # 建立字典 dic_city = req.json() #print(dic_city) #獲取資料 city_data = dic_city.get('data') def speak(t=''): result = client.synthesis(t, 'zh', 0.5, {'vol': 5, 'per': 4}) if not isinstance(result, dict): with open('天氣.mp3', 'wb') as f: f.write(result) #建立輸出函式 def shuchu(): print('日期:', city_forecast.get('date')) print('高溫:', city_forecast.get('high')) print('低溫:', city_forecast.get('low')) print('天氣:', city_forecast.get('type')) print('風力:', city_forecast.get('fengli')) print('風向:', city_forecast.get('fengxiang')) print('注意:', city_data.get('ganmao')) speak(city+'天氣'+city_forecast.get('date')\ +city_forecast.get('high')+city_forecast.get('low')\ +city_forecast.get('type')+city_forecast.get('fengli')\ +city_forecast.get('fengxiang')+city_data.get('ganmao')) os.system('天氣.mp3') #開啟音訊檔案播放 if city_data: if data == str('當天'): city_forecast = city_data['forecast'][0] shuchu() if data == str('明天'): city_forecast = city_data['forecast'][1] shuchu() if data == str('後天'): city_forecast = city_data['forecast'][2] shuchu() if data == str('大後天'): city_forecast = city_data['forecast'][3] shuchu() if data == str('第五天'): city_forecast = city_data['forecast'][4] shuchu() else: print('未獲得') breakinput('請按回車鍵退出')