Python天氣語音播報(bào)小助手
導(dǎo)語
馬上就要迎來國慶小長假了~激不激動(dòng),興不興奮!
那今年國慶:天氣怎么樣?能不能出門逛街?能不能出去旅游?
……
來來來,木木子為你整理好啦!這個(gè)假期,你那里的天氣如何?
正文
旅游出門就要挑個(gè)好的天氣!下雨天哪兒哪兒都不舒服。
今天小編帶大家寫一款Python天氣語音播報(bào)小助手!
環(huán)境安裝:Python3.6、pycharm2021、及自帶的模塊等。
pip install -i https://pypi.douban.com/simple/ requests pip install -i https://pypi.douban.com/simple/ opencv-python
主要分為三大部分:
(1)獲取每日天氣情況:
def get_weather(): url = 'http://www.weather.com.cn/weather/101290101.shtml' response = requests.get(url) response.encoding = 'utf-8' response = response.text # 獲取頁面 html = etree.HTML(response) day_weather = '天氣狀況:' + html.xpath('//*[@id="7d"]/ul/li[1]/p[1]/text()')[0] + '\n' # 獲取天氣,白天的天氣 high = html.xpath('//*[@id="7d"]/ul/li[1]/p[2]/span/text()') low = html.xpath('//*[@id="7d"]/ul/li[1]/p[2]/i/text()') # 獲取對(duì)應(yīng)的兩個(gè)溫度 # 因?yàn)轫撁嬖谕砩蠒?huì)有小變化,所以使用條件語句,來排除因變化引起的bug if high == []: day_temperature = '室外溫度:' + low[0] + '\n' else: day_temperature = '室外溫度:' + low[0].replace('℃', '') + '~' + high[0] + '℃\n' # 獲取溫度 # 獲取兩個(gè)風(fēng)向 wind_1 = html.xpath('//*[@id="7d"]/ul/li[1]/p[3]/em/span[1]/@title') wind_2 = html.xpath('//*[@id="7d"]/ul/li[1]/p[3]/em/span[2]/@title') # 因?yàn)橛袝r(shí)候,會(huì)出現(xiàn)兩個(gè)風(fēng)向是同一個(gè)風(fēng)向的情況,所以使用條件語句排除 if wind_2 == []: wind = wind_1[0] + '\n' elif wind_1[0] == wind_2[0]: wind = wind_1[0] + '\n' else: wind = wind_1[0] + '轉(zhuǎn)' + wind_2[0] + '\n' # 因?yàn)轱L(fēng)級(jí)有時(shí)候會(huì)出現(xiàn)“<",語音的時(shí)候會(huì)認(rèn)為是愛心符號(hào),所以使用替換,改為文字”低于“ wind_3 = html.xpath('//*[@id="7d"]/ul/li[1]/p[3]/i/text()')[0].replace('<', '低于').replace('>', '高于') day_wind = '風(fēng)向情況:' + wind + wind_3 + '\n' # 獲取風(fēng)向及風(fēng)級(jí) return day_weather, day_temperature, day_wind
(2)獲取播報(bào)的高考時(shí)間:
def get_time(): a = datetime.datetime.now() # 實(shí)施時(shí)間 y = str(a.year) m = str(a.month) d = str(a.day) # 轉(zhuǎn)換為字符串,便于打印 time = y + '年' + m + '月' + d + '日' + '\n' b = datetime.datetime(2021, 6, 7) # 自己設(shè)置的高考時(shí)間 count_down = (b - a).days # 高考倒計(jì)時(shí) return time, count_down
(3)設(shè)置播報(bào)每日雞湯文字:
def get_content(): url = 'http://open.iciba.com/dsapi/' # 網(wǎng)上找的API response = requests.get(url=url) json_s = json.loads(response.text) jitang = json_s.get("content") + '\n' # 每日雞湯 translation = json_s.get("note") + '\n' # 中文翻譯 image_url = json_s.get("fenxiang_img") # 圖片鏈接 return jitang, translation, image_url
(4)語音小助手依次順序播報(bào):
def main(): time, count_down = get_time() day_weather, day_temperature, day_wind = get_weather() jitang, translation, image_url = get_content() count_down = '距離高考還有{}天,你準(zhǔn)備好了嗎?'.format(count_down) + '\n' a = '下面為您播報(bào)今日天氣狀況\n' b = '每日一句\n' time = '今天是' + time weather = day_weather + day_temperature + day_wind content = jitang + translation text = time + count_down + a + weather + b + content # 語音內(nèi)容 voice = pyttsx3.init() # 初始化 # rate = voice.getProperty('rate') voice.setProperty('rate', 150) # 語速,范圍在0-200之間 voice.setProperty('volume', 1.0) # 范圍在0.0-1.0之間 voice.say(text) # 語音內(nèi)容 voice.runAndWait() cap = cv2.VideoCapture(image_url) # 展示圖片 if(cap.isOpened()): ret, img = cap.read() my_image = cv2.resize(img, dsize=None, fx=0.5, fy=0.5) cv2.imshow("You will succeed in the end", my_image) cv2.waitKey() print(time, weather, content)
效果如下:
其實(shí)是語音播報(bào)的,but這只能截圖效果將就著看叭~哈哈哈?。?!
總結(jié)
好啦!這是一款實(shí)時(shí)播報(bào)、高考、天氣預(yù)報(bào)、每日雞湯的三合一語音智能小助手!想擁有嘛?
記得三連哦~mua 你們的支持是我最大的動(dòng)力!
到此這篇關(guān)于Python天氣語音播報(bào)小助手的文章就介紹到這了,更多相關(guān)Python 語音播報(bào) 內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。