Python如何实现天气语音播报小助手
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍了Python如何实现天气语音播报小助手,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。环境安装:Python3.6、p
千家信息网最后更新 2026年11月01日Python如何实现天气语音播报小助手
这篇文章主要介绍了Python如何实现天气语音播报小助手,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
环境安装: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') # 因为有时候,会出现两个风向是同一个风向的情况,所以使用条件语句排除 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' # 因为风级有时候会出现"<",语音的时候会认为是爱心符号,所以使用替换,改为文字"低于" 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/' # 网上找的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 # 语音内容 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) # 语音内容 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这只能截图效果将就着看叭~哈哈哈!!!
感谢你能够认真阅读完这篇文章,希望小编分享的"Python如何实现天气语音播报小助手"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
天气
语音
风向
温度
篇文章
高考
助手
两个
情况
时间
之间
内容
图片
效果
文字
有时候
条件
状况
范围
语句
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中智农互联网科技
湖北九亚互联网络科技有限公司
上海睿民互联网科技有限公司
科技与互联网
善班互联网科技
互联网科技公司官网
互联网科技视频
互联网金融科技委员会
互联网科技公司需要的职位
北京大学互联网科技怎么样
广州慧亚互联网科技怎么样
南山科技园互联网公司有哪些
华夏互联网科技业
互联网新科技动漫ppt素材
互联网行业科技股
黑云互联网科技有限公司
工业互联网科技成果转化
云南互联网科技学费
10位科技互联网大佬
广州全民伙伴互联网科技有限公司
科技互联网项目
北京互联网科技有限公司
科技互联网创业计划书
湖南一路畅行互联网科技
知域互联网科技有限公司大骗子
烟台灵鸽互联网科技有限公司
互联网公司属于科技吗
互联网金融运营模式中的金融科技
北京互联网科技类企业分布
网互联网络科技