定制自己的天氣預(yù)報(bào)小助手。
想體驗(yàn)一下智能家居的生活,用python寫(xiě)個(gè)腳本,每天早上獲取當(dāng)?shù)靥鞖馇闆r,并自定義播報(bào)內(nèi)容。
import requests
import pyttsx3
import json
url = "http://www.weather.com.cn/data/sk/101010300.html"
#查詢當(dāng)前天氣朝陽(yáng)id101010300
cur_tq = requests.get(url)
cur_tq.encoding = "utf-8"
curtqjson = json.loads(cur_tq.text)
#str 轉(zhuǎn)json
local = curtqjson['weatherinfo']['city']
temp = curtqjson['weatherinfo']['temp']
weath = curtqjson['weatherinfo']['WD']
#語(yǔ)音轉(zhuǎn)文字
e = pyttsx3.init()
e.say("主人您好,您當(dāng)前所處的位置是"+local+"溫度"+temp+"天氣"+weath)
e.runAndWait()