alarm_hour=alarm_time[0:2]
alarm_minute=alarm_time[3:5]
alarm_seconds=alarm_time[6:8]
alarm_period = alarm_time[9:11].upper()
print(“Setting up alarm…”)
while True:
now = datetime.now()
current_hour = now.strftime(“%I”)
current_minute = now.strftime(“%M”)
current_seconds = now.strftime(“%S”)
current_period = now.strftime(“%p”)
if(alarm_periodcurrent_period):
if(alarm_hourcurrent_hour):
if(alarm_minutecurrent_minute):
if(alarm_secondscurrent_seconds):
print(“Wake Up!”)
playsound(‘audio.mp3’) ## download the alarm sound from link
break
⑬ 有声读物
目的:编写一个Python脚本,用于将Pdf文件转换为有声读物。
提示:借助pyttsx3库将文本转换为语音。
安装:pyttsx3,PyPDF2
![在这里插入图片描述](https://img-blog.csdnimg.cn/a92254ae94034ebb8ce8bc2f3f2427fc.png)
⑭ 天气应用
目的:编写一个Python脚本,接收城市名称并使用爬虫获取该城市的天气信息。
提示:你可以使用Beautifulsoup和requests库直接从谷歌主页爬取数据。
安装:requests,BeautifulSoup
from bs4 import BeautifulSoup
import requests
headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3’}
def weather(city):
city=city.replace(" “,”+")
res = requests.get(f’https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8’,headers=headers)
print(“Searching in google…\n”)
soup = BeautifulSoup(res.text,‘html.parser’)
location = soup.select(‘#wob_loc’)[0].getText().strip()
time = soup.select(‘#wob_dts’)[0].getText().strip()
info = soup.select(‘#wob_dc’)[0].getText().strip()
weather = soup.select(‘#wob_tm’)[0].getText().strip()
print(location)
print(time)
print(info)
print(weather+“°C”)
print(“enter the city name”)
city=input()
city=city+" weather"
weather(city)
⑮ 人脸检测
目的:编写一个Python脚本,可以检测图像中的人脸,并将所有的人脸保存在一个文件夹中。
提示:可以使用haar级联分类器对人脸进行检测。它返回的人脸坐标信息,可以保存在一个文件中。
安装:OpenCV。
下载:haarcascade\_frontalface\_default.xml
import cv2
Load the cascade
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)