需要用到的头文件
import requests import datetime import time import json import re import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart
获取天气
免费的天气API:链接直达
def getWeather(city): appid = "网站注册的appid" appsecret = "在网站注册中获取" temp = requests.get( url="https://v0.yiketianqi.com/api?version=v61&appid=" + appid + "&appsecret=" + appsecret + "&city=" + city, timeout=5 ) weather = temp.json()['wea'] + " " + temp.json()['tem'] + "度" # 天气 + 温度 air_tips = temp.json()['air_tips'] # 小贴士 today = temp.json()['date'] + " " + temp.json()['week'] # 今天时间 update = temp.json()['update_time'] # 温度更新时间 hWather = temp.json()['tem1'] + "度" # 高温 lWather = temp.json()['tem2'] + "度" # 低温 value = city + "天气和温度:" + weather + "\n" + \ "当前时间:" + today + "\n" + \ "最高温度:" + hWather + "\n" + \ "最低温度:" + lWather + "\n" + \ "天气更新时间:" + update + "\n" + \ "小贴士:" + air_tips return value
除了appid和appsecret 需要改成自己的其余的不需要改动
发送邮件
def sendMail(msg, text): # 发送邮件的邮箱 sender = '用来发送邮件的邮箱@qq.com' # 接受邮件的邮箱 to_list = [ '发送目标邮箱1@qq.com', '发送目标邮箱2@qq.com' #一封发送给自己一封发送给女朋友 #以确保发送内容没有错误以及定时发送是否执行 ] subject = text # 创建邮箱 em = MIMEMultipart() em['subject'] = subject em['From'] = sender em['To'] = ",".join(to_list) # 邮件的内容 content = MIMEText(msg) em.attach(content) # 发送邮件 # 1、连接服务器 # 25端口已经被云服务器商关闭了,所以只能用465端口了 smtp = smtplib.SMTP_SSL('smtp.qq.com', 465) # 2、登录 smtp.login(sender, "qq邮箱授权码") print("登录服务器成功") # 3、发邮件 smtp.send_message(em) print("发送邮件成功") # 4、关闭连接 smtp.close()
具体操作可以参考我的另一篇文章:Python发送邮箱
定时发送
def timeSend(): startTime = datetime.datetime.now() if startTime.hour == 7 and startTime.minute <= 1: # 每天9点定时运行执行晚上函数 hello = everyDayHello() sendMail(getWeather('地区') + "\n\n" + str(everyDayNight())+"\n\n" + hello[0]['en'] + "\n" + hello[0]['zh'], '来至男友每天早上的关爱') print("早上信息已发送") time.sleep(80) if startTime.hour == 22 and startTime.minute <= 1: # 每天20点定时运行执行晚上函数 hello = everyDayHello() sendMail(getWeather('地区') + "\n\n" + str(everyDayNight())+"\n\n" + hello[0]['en'] + "\n" + hello[0]['zh'], '来至男友每天晚上的关爱') print("晚上信息已发送") time.sleep(80) def main(): print("start") while True: timeSend() time.sleep(30)
运行过后需要测试一下是否能准确时间发送内容可以通过调整三个time.sleep的时间来测试
每日一语
这里我用的api是天行数据,同样免费:链接直达
key = '你自己的key' def everyDayMorning(): temp = requests.get( 'http://api.tianapi.com/txapi/zaoan/index?key='+key) return temp.json()["newslist"][0]['content'] # 晚安函数 def everyDayNight(): temp = requests.get( 'http://api.tianapi.com/txapi/wanan/index?key='+key) return temp.json()["newslist"][0]['content'] def everyDayHello(): temp = requests.get( 'http://api.tianapi.com/txapi/ensentence/index?key='+key) return temp.json()["newslist"]
完整项目代码
import requests import datetime import time import json import re import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # 邮箱配置 def sendMail(msg, text): # 发送邮件的邮箱 sender = '用来发送邮件的邮箱@qq.com' # 接受邮件的邮箱 to_list = [ '发送目标邮箱1@qq.com', '发送目标邮箱2@qq.com' #一封发送给自己一封发送给女朋友 #以确保发送内容没有错误以及定时发送是否执行 ] subject = text # 创建邮箱 em = MIMEMultipart() em['subject'] = subject em['From'] = sender em['To'] = ",".join(to_list) # 邮件的内容 content = MIMEText(msg) em.attach(content) # 发送邮件 # 1、连接服务器 # 25端口已经被云服务器商关闭了,所以只能用465端口了 smtp = smtplib.SMTP_SSL('smtp.qq.com', 465) # 2、登录 smtp.login(sender, "qq邮箱授权码") print("登录服务器成功") # 3、发邮件 smtp.send_message(em) print("发送邮件成功") # 4、关闭连接 smtp.close() #天气模块 def getWeather(city): appid = "网站注册的appid" appsecret = "在网站注册中获取" temp = requests.get( url="https://v0.yiketianqi.com/api?version=v61&appid=" + appid + "&appsecret=" + appsecret + "&city=" + city, timeout=5 ) weather = temp.json()['wea'] + " " + temp.json()['tem'] + "度" # 天气 + 温度 air_tips = temp.json()['air_tips'] # 小贴士 today = temp.json()['date'] + " " + temp.json()['week'] # 今天时间 update = temp.json()['update_time'] # 温度更新时间 hWather = temp.json()['tem1'] + "度" # 高温 lWather = temp.json()['tem2'] + "度" # 低温 value = city + "天气和温度:" + weather + "\n" + \ "当前时间:" + today + "\n" + \ "最高温度:" + hWather + "\n" + \ "最低温度:" + lWather + "\n" + \ "天气更新时间:" + update + "\n" + \ "小贴士:" + air_tips return value # 语句函数 key = '你自己的key' def everyDayMorning(): temp = requests.get( 'http://api.tianapi.com/txapi/zaoan/index?key='+key) return temp.json()["newslist"][0]['content'] def everyDayNight(): temp = requests.get( 'http://api.tianapi.com/txapi/wanan/index?key='+key) return temp.json()["newslist"][0]['content'] def everyDayHello(): temp = requests.get( 'http://api.tianapi.com/txapi/ensentence/index?key='+key) return temp.json()["newslist"] # 定时发送 def timeSend(): startTime = datetime.datetime.now() if startTime.hour == 7 and startTime.minute <= 1: # 每天9点定时运行执行晚上函数 hello = everyDayHello() sendMail(getWeather('地区') + "\n\n" + str(everyDayNight())+"\n\n" + hello[0]['en'] + "\n" + hello[0]['zh'], '来至男友每天早上的关爱') print("早上信息已发送") time.sleep(80) if startTime.hour == 22 and startTime.minute <= 1: # 每天20点定时运行执行晚上函数 hello = everyDayHello() sendMail(getWeather('地区') + "\n\n" + str(everyDayNight())+"\n\n" + hello[0]['en'] + "\n" + hello[0]['zh'], '来至男友每天晚上的关爱') print("晚上信息已发送") time.sleep(80) #主函数 def main(): while True: timeSend() time.sleep(10) if __name__ == "__main__": main()
祝各位网友情节人快乐,牛年发大财,程序无BUG!!