python也可以成为贴心小管家--每日播报考研倒计时、天气情况、还有“营养鸡汤”

简介: 简介:对于考研,你还在每天数着还有多少天吗?对于天气,你还每天去看天气预报吗?你每天需要看一些励志话语来督促自己前进吗?python可以合上述三者为一体帮你轻松实现你的日常!

python也可以成为贴心小管家--每日播报考研倒计时、天气情况、还有“营养鸡汤”


一、前言


对于考研,你还在每天数着还有多少天吗?对于天气,你还每天去看天气预报吗?你每天需要看一些励志话语来督促自己前进吗?

python可以合上述三者为一体帮你轻松实现你的日常!

请看下面详解。


二、需要导入的库


import requests
from lxml import etree
import json
import cv2
import pyttsx3
import datetime


三、分析


1、考研倒计时

对于倒计时,我使用了datetime库,得到实时时间,并拆分出年份、月份和日期,自己设置研究生考试时间,然后进行两者相减。函数代码如下:


# 获取日期和倒计时
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, 12, 25)  # 自己设置的研究生考试时间
    count_down = (b - a).days  # 考研倒计时
    return time, count_down


2、天气情况

以南通为例,网页的链接为:http://www.weather.com.cn/weather/101190501.shtml

(如果需要切换成其它的城市,把上述链接的101190501,改为需要切换的城市的id即可。在上述链接页面,右击打开检查页面之后,下图为详细步骤图)


image.png


实现代码:


def get_weather():
    url = 'http://www.weather.com.cn/weather/101190501.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


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

四、完整代码


在主函数中,使用pyttsx3库实现语音播报功能,使用cv2实现图片的展示功能。

代码如下:

# -*- coding: UTF-8 -*-
"""
@Author  :远方的星
@Time   : 2021/3/10 16:44
@CSDN    :https://blog.csdn.net/qq_44921056
@腾讯云   : https://cloud.tencent.com/developer/column/91164
"""
import requests
from lxml import etree
import json
import cv2
import pyttsx3
import datetime
# 获取日期和倒计时
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, 12, 25)  # 自己设置的研究生考试时间
    count_down = (b - a).days  # 考研倒计时
    return time, count_down
# 获取南通当日天气情况
def get_weather():
    url = 'http://www.weather.com.cn/weather/101190501.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
# 获取每日鸡汤
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
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)
if __name__ == "__main__":
    main()



相关文章
|
Python
Python实现简易天气查询系统
Python实现简易天气查询系统
803 4
|
存储 数据挖掘 数据库
【Python】python天气数据抓取与数据分析(源码+论文)【独一无二】
【Python】python天气数据抓取与数据分析(源码+论文)【独一无二】
|
传感器 存储 人工智能
用通义灵码2.5打造智能倒计时日历:从零开始的Python开发体验
本文记录了使用通义灵码2.5开发倒计时日历工具的全过程,展现了其智能体模式带来的高效协作体验。从项目构思到功能实现,通义灵码不仅提供了代码生成与补全,还通过自主决策分解需求、优化界面样式,并集成MCP工具扩展功能。其记忆能力让开发流程更连贯,显著提升效率。最终成果具备事件管理、天气预报等功能,界面简洁美观。实践证明,通义灵码正从代码补全工具进化为真正的智能开发伙伴。
python tkinter 界面倒计时(用于监督页面返回)
python tkinter 界面倒计时(用于监督页面返回)
python tkinter 界面倒计时(用于监督页面返回)
|
数据采集 数据可视化 安全
基于python大数据的天气可视化分析预测系统
本研究探讨基于Python的天气预报数据可视化系统,旨在提升天气数据获取、分析与展示的效率与准确性。通过网络爬虫技术快速抓取实时天气数据,并运用数据可视化技术直观呈现天气变化趋势,为公众出行、农业生产及灾害预警提供科学支持,具有重要的现实意义与应用价值。
|
数据可视化 前端开发 数据挖掘
【优秀python大屏】基于python flask的广州历史天气数据应用与可视化大屏
本文介绍了一个基于Python Flask框架的广州历史天气数据应用与可视化大屏系统,该系统通过数据采集、处理、分析和可视化技术,提供了丰富的气象数据展示和决策支持,帮助用户快速了解和应对气象变化。
933 110
【优秀python大屏】基于python flask的广州历史天气数据应用与可视化大屏
|
存储 小程序 Python
农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序
### 农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序 该程序通过`lunardate`库实现公历与农历的日期转换,支持闰月和跨年处理,用户输入农历节日名称后,可准确计算距离该节日还有多少天。功能包括农历节日查询、倒计时计算等。欢迎使用! (239字符)
1569 86
|
数据采集 JSON 数据可视化
基于Python的全国主要城市天气数据可视化大屏系统
本文介绍了一个基于Python开发的全国主要城市天气数据可视化大屏系统,该系统利用requests库进行数据采集、Pandas进行数据处理与分析,并采用Echarts和Flask实现数据的可视化展示和Web交互,以直观、实时地呈现天气变化趋势。
1327 2
基于Python的全国主要城市天气数据可视化大屏系统
|
机器学习/深度学习 数据采集 传感器
使用Python实现深度学习模型:智能极端天气事件预测
使用Python实现深度学习模型:智能极端天气事件预测
1291 3
|
机器学习/深度学习 数据可视化 TensorFlow
使用Python实现深度学习模型:智能天气预测与气候分析
使用Python实现深度学习模型:智能天气预测与气候分析
2407 3

推荐镜像

更多