2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Python贴心小管家源代码--每日播报高考倒计时 天气情况 还有“营养鸡汤”

Python贴心小管家源代码--每日播报高考倒计时 天气情况 还有“营养鸡汤”

时间:2021-05-15 11:50:50

相关推荐

Python贴心小管家源代码--每日播报高考倒计时 天气情况 还有“营养鸡汤”

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

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

运行截图:

很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。

很多已经做案例的人,却不知道如何去学习更加高深的知识。

那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!

QQ群:701698587

一、需要导入的库

import requestsfrom lxml import etreeimport jsonimport cv2import pyttsx3importdatetime

二、各模块实现

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

2、获取天气情况

以昆明为例,爬取网页的链接为:/weather/101290101.shtml

(如果需要切换成其它的城市,把上述链接的101290101,改为需要切换的城市的id即可。)

def get_weather():url = '/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' # 获取风向及风级returnday_weather,day_temperature,day_wind

3、营养鸡汤

为了趣味性,并且顺便练习听力,我爬取了《爱词霸》的中英文每日一句,并且将图片链接爬取,进行展示(图片不会保存,随着代码运行结束,图片也会消失)因为网上有接口,所以直接借助接口之间爬取。

def get_content():url = '/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") # 图片链接returnjitang,translation,image_url

三、完整代码

在主函数中,使用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(, 6, 7) # 自己设置的高考时间count_down = (b - a).days # 高考倒计时return time, count_down# 获取昆明当日天气情况def get_weather():url = '/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 = '/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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。