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

文章插图
文章插图
?
一、需要导入的库
import requestsfrom lxml import etreeimport jsonimport cv2import pyttsx3import 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/101290101.shtml
(如果需要切换成其它的城市,把上述链接的101290101,改为需要切换的城市的id即可 。)
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()')# 获取对应的两个温度# 因为页面在晚上会有小变化,所以使用条件语句,来排除因变化引起的bugif 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/'# 网上找的APIresponse = 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文章插图
【PYTHON CAD绘图 Python贴心小管家源代码--每日播报高考倒计时、天气情况、还有“营养鸡汤”】三、完整代码
在主函数中,使用pyttsx3库实现语音播报功能,使用cv2实现图片的展示功能 。
代码如下:
# -*- coding: UTF-8 -*-import requestsfrom lxml import etreeimport jsonimport cv2import pyttsx3import 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, 6, 7)# 自己设置的高考时间count_down = (b - a).days# 高考倒计时return time, count_down# 获取昆明当日天气情况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()')# 获取对应的两个温度# 因为页面在晚上会有小变化,所以使用条件语句,来排除因变化引起的bugif 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/'# 网上找的APIresponse = 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_urldef 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 = '今天是' + timeweather = day_weather + day_temperature + day_windcontent = jitang + translationtext = 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()文章插图
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
