Python天氣語音播報小助手
導語
馬上就要迎來國慶小長假了~激不激動,興不興奮!
那今年國慶:天氣怎么樣?能不能出門逛街?能不能出去旅游?
……
來來來,木木子為你整理好啦!這個假期,你那里的天氣如何?

正文
旅游出門就要挑個好的天氣!下雨天哪兒哪兒都不舒服。
今天小編帶大家寫一款Python天氣語音播報小助手!
環(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()') # 獲取對應的兩個溫度
# 因為頁面在晚上會有小變化,所以使用條件語句,來排除因變化引起的bug
if high == []:
day_temperature = '室外溫度:' + low[0] + '\n'
else:
day_temperature = '室外溫度:' + low[0].replace('℃', '') + '~' + high[0] + '℃\n' # 獲取溫度
# 獲取兩個風向
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')
# 因為有時候,會出現(xiàn)兩個風向是同一個風向的情況,所以使用條件語句排除
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] + '轉' + wind_2[0] + '\n'
# 因為風級有時候會出現(xiàn)“<",語音的時候會認為是愛心符號,所以使用替換,改為文字”低于“
wind_3 = html.xpath('//*[@id="7d"]/ul/li[1]/p[3]/i/text()')[0].replace('<', '低于').replace('>', '高于')
day_wind = '風向情況:' + wind + wind_3 + '\n' # 獲取風向及風級
return day_weather, day_temperature, day_wind
(2)獲取播報的高考時間:
def get_time():
a = datetime.datetime.now() # 實施時間
y = str(a.year)
m = str(a.month)
d = str(a.day) # 轉換為字符串,便于打印
time = y + '年' + m + '月' + d + '日' + '\n'
b = datetime.datetime(2021, 6, 7) # 自己設置的高考時間
count_down = (b - a).days # 高考倒計時
return time, count_down
(3)設置播報每日雞湯文字:
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)語音小助手依次順序播報:
def main():
time, count_down = get_time()
day_weather, day_temperature, day_wind = get_weather()
jitang, translation, image_url = get_content()
count_down = '距離高考還有{}天,你準備好了嗎?'.format(count_down) + '\n'
a = '下面為您播報今日天氣狀況\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)
效果如下:

其實是語音播報的,but這只能截圖效果將就著看叭~哈哈哈?。?!

總結
好啦!這是一款實時播報、高考、天氣預報、每日雞湯的三合一語音智能小助手!想擁有嘛?
記得三連哦~mua 你們的支持是我最大的動力!

到此這篇關于Python天氣語音播報小助手的文章就介紹到這了,更多相關Python 語音播報 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PyTorch中的squeeze()和unsqueeze()解析與應用案例
這篇文章主要介紹了PyTorch中的squeeze()和unsqueeze()解析與應用案例,文章內(nèi)容介紹詳細,需要的小伙伴可以參考一下,希望對你有所幫助2022-03-03
分析Python編程時利用wxPython來支持多線程的方法
這篇文章主要介紹了Python編程時利用wxPython來支持多線程的方法,本文主要以開發(fā)GUI程序時做線程通訊作為一個示例來講解,需要的朋友可以參考下2015-04-04
Pandas庫中dataframe.corr()函數(shù)的使用
dataframe.corr()是Pandas庫中的一個函數(shù),用于計算DataFrame中各列之間的相關系數(shù),本文主要介紹了Pandas庫中dataframe.corr()函數(shù)的使用,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Python爬取股票信息,并可視化數(shù)據(jù)的示例
這篇文章主要介紹了Python爬取股票信息,并可視化數(shù)據(jù)的示例,幫助大家更好的理解和使用python爬蟲,感興趣的朋友可以了解下2020-09-09

