python語音天氣預(yù)報(bào)

剛學(xué)習(xí)python,看大神各種操作,想著自己也寫一個(gè)小程序。每天早上起來第一件事就是開電腦,于是就寫了一個(gè)語音播報(bào)天氣的小程序。

看他人都是用的爬蟲抓取的天氣,自己還不會(huì),于是就用了api,在京東花一分錢買的api,

開始訪問api,用到了?requests,特別簡(jiǎn)單

代碼:

import requests

r = requests.get('https://way.jd.com/jisuapi/weather?city=吉林&cityid=111&citycode=101260301&appkey=6f9f4bf8c5d672c29aee8b5992bc20a9')

r.encoding = 'utf-8'? #不加這行有可能會(huì)亂碼

print(r.text)

打印返回?cái)?shù)據(jù),得到如下結(jié)果


返回的事json格式,于是用了一下json在線解析


json解析


這樣就好看多了

然后對(duì)里面的內(nèi)容進(jìn)行提取

date=r.json()["result"]['result']["date"]

templow=r.json()["result"]['result']["templow"]

temphigh=r.json()["result"]['result']["temphigh"]

tempnow=r.json()["result"]['result']["temp"]

week=r.json()["result"]['result']["week"]

tip=r.json()["result"]['result']["index"][3]["detail"]

weather=r.json()["result"]['result']['daily'][0]['night']['weather']

add=r.json()["result"]['result']["city"]

wind=r.json()["result"]['result']['winddirect']

WS=r.json()["result"]['result']["windpower"]

再導(dǎo)入時(shí)間模塊,獲取當(dāng)前時(shí)間

import time

t = time.localtime() # 當(dāng)前時(shí)間的紀(jì)元值

fmt = "%H %M"

now = time.strftime(fmt, t)? # 將紀(jì)元值轉(zhuǎn)化為包含時(shí)、分的字符串

now = now.split(' ') #以空格切割,將時(shí)、分放入名為now的列表中

hour = now[0]

minute = now[1]


整理一下,

wea='你好,,今天是%s%s,現(xiàn)在北京時(shí)間%s時(shí)%s分,%s天氣%s,氣溫%s攝氏度~%s攝氏度,現(xiàn)在為%s攝氏度,%s,風(fēng)力%s,%s'%(date,week,hour,minute,add,weather,templow,temphigh,tempnow,wind,WS,tip)

運(yùn)行的結(jié)果就是:

你好,,今天是2017-12-14星期四,現(xiàn)在北京時(shí)間22時(shí)54分,吉林市天氣晴,氣溫-19攝氏度~-13攝氏度,現(xiàn)在為-13攝氏度,南風(fēng),風(fēng)力2級(jí),晝夜溫差很大,易發(fā)生感冒,請(qǐng)注意適當(dāng)增減衣服,加強(qiáng)自我防護(hù)避免感冒。

這一步已經(jīng)做好,現(xiàn)在開始將文字轉(zhuǎn)化為語音。

這里用的事百度的語音合成api百度在線語音合成 Python SDK參考文檔

from aip import AipSpeech

APP_ID = '***************'

API_KEY = '*******************************'

SECRET_KEY = '*******************************'

aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result? = aipSpeech.synthesis(wea, 'zh', 1, {

'vol': 10,'per':4,'spd':5

})

# 識(shí)別正確返回語音二進(jìn)制 錯(cuò)誤則返回dict 參照下面錯(cuò)誤碼

if not isinstance(result, dict):

with open('auido.mp3', 'wb') as f:

f.write(result)

播放mp3文件沒找很好的方式,學(xué)的太次了,用的pygame

#播放語音的pygame

pygame.mixer.init(frequency=16000,size=-16,channels=4)

track = pygame.mixer.music.load('auido.mp3')

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

pygame.time.delay(100)

pygame.mixer.quit()

整個(gè)程序就寫完了,很少,但是好像讓我寫的很麻煩,第一次寫。

但是怎么能讓電腦開機(jī)播放呢,這里用到了任務(wù)計(jì)劃


任務(wù)計(jì)劃


這個(gè)在控制面板->管理工具->任務(wù)計(jì)劃程序,創(chuàng)建任務(wù),在操作中要做如下配置

任務(wù)計(jì)劃配置


程序那里要填python的安裝目錄,添加參數(shù)要填程序的目錄,起始于空著就好。


觸發(fā)器


觸發(fā)器我是這樣配置的,每天早上7點(diǎn)45準(zhǔn)時(shí)播報(bào),還有每次輸入密碼的時(shí)候也就是開機(jī)的時(shí)候,但是我每次電腦關(guān)機(jī)之前都是靜音的狀態(tài),所以,在開機(jī)之后拖十分鐘啟動(dòng),保證網(wǎng)絡(luò)的順暢。

于是,開機(jī)語音播報(bào)天氣的功能就實(shí)現(xiàn)了。

文中有錯(cuò)誤的地方,敬請(qǐng)指正。

希望可以和各位一起學(xué)習(xí)。

放完整源碼:

import requests

import time

import pygame

#抓取中國天氣網(wǎng)吉林天氣

r = requests.get('https://way.jd.com/jisuapi/weather?city=吉林&cityid=111&citycode=101260301&appkey=6f9f4bf8c5d672c29aee8b5992bc20a9')

r.encoding = 'utf-8'

# print(r.text)

date=r.json()["result"]['result']["date"]

templow=r.json()["result"]['result']["templow"]

temphigh=r.json()["result"]['result']["temphigh"]

tempnow=r.json()["result"]['result']["temp"]

week=r.json()["result"]['result']["week"]

tip=r.json()["result"]['result']["index"][3]["detail"]

weather=r.json()["result"]['result']['daily'][0]['night']['weather']

add=r.json()["result"]['result']["city"]

wind=r.json()["result"]['result']['winddirect']

WS=r.json()["result"]['result']["windpower"]

t = time.localtime()? # 當(dāng)前時(shí)間的紀(jì)元值

fmt = "%H %M"

now = time.strftime(fmt, t)? # 將紀(jì)元值轉(zhuǎn)化為包含時(shí)、分的字符串

now = now.split(' ') #以空格切割,將時(shí)、分放入名為now的列表中

hour = now[0]

minute = now[1]

wea='你好,龐樹博,今天是%s%s,現(xiàn)在北京時(shí)間%s時(shí)%s分,%s天氣%s,氣溫%s攝氏度~%s攝氏度,現(xiàn)在為%s攝氏度,%s,風(fēng)力%s,%s'%(date,week,hour,minute,add,weather,templow,temphigh,tempnow,wind,WS,tip)

print(wea)

#百度語音合成apI

from aip import AipSpeech

""" App ID: 9946043

API Key: WpcSAMtagA6HtrNsmTQi1GFa

Secret Key: a89f277902098c1ed872f566c2e0e50c """

APP_ID = '9946043'

API_KEY = 'WpcSAMtagA6HtrNsmTQi1GFa'

SECRET_KEY = 'a89f277902098c1ed872f566c2e0e50c'

aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result? = aipSpeech.synthesis(wea, 'zh', 1, {

'vol': 10,'per':4,'spd':5

})

# 識(shí)別正確返回語音二進(jìn)制 錯(cuò)誤則返回dict 參照下面錯(cuò)誤碼

if not isinstance(result, dict):

with open('auido.mp3', 'wb') as f:

f.write(result)

#播放語音的pygame

pygame.mixer.init(frequency=16000,size=-16,channels=4)

track = pygame.mixer.music.load('auido.mp3')

pygame.mixer.music.play()

while pygame.mixer.music.get_busy():

pygame.time.delay(100)

pygame.mixer.quit()

“我喜歡程序員,他們單純、固執(zhí)、容易體會(huì)到成就感;面對(duì)壓力,能夠挑燈夜戰(zhàn)不眠不休;面對(duì)困難,能夠迎難而上挑戰(zhàn)自我。他們也會(huì)感到困惑與傍徨,但每個(gè)程序員的心中都有一個(gè)比爾蓋茨或是喬布斯的夢(mèng)想“用智慧開創(chuàng)屬于自己的事業(yè)”。我想說的是,其實(shí)我是一個(gè)程序員”

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容