ADD file via upload

master
hnu202210040318 2 years ago
parent ff0c28ae21
commit 718c02ed09

@ -0,0 +1,109 @@
#模块
import time , requests , json , time , random , smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def GetWeatherNow(Code):
url = f"https://api.map.baidu.com/weather/v1/?district_id={Code}&data_type=all&ak=jnQIyILmGqPux9WAIzXoks1gHxkV7aTO"
header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
response = requests.get(url, headers=header)
data = response.text
data_json = json.loads(data)
province = data_json["result"]["location"]["province"]
city = data_json["result"]["location"]["city"]
destrict = data_json["result"]["location"]["name"]
location = province + city + destrict + ""
weather = data_json["result"]["now"]["text"]
temperature = data_json["result"]["now"]["temp"]
wind = data_json["result"]["now"]["wind_dir"]
windscale = data_json["result"]["now"]["wind_class"]
time0 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
Msg1 = f"您好,现在为您进行天气预报:\n您所在的位置是:{location}\n当前天气:{weather}\n当前温度:{temperature}\n风力:{wind}{windscale}\n现在时间是:{time0}"
return Msg1
#爬取即时天气预报
def GetWeatherForcast(Code):
url = f"https://api.map.baidu.com/weather/v1/?district_id={Code}&data_type=all&ak=jnQIyILmGqPux9WAIzXoks1gHxkV7aTO"
header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"}
response = requests.get(url, headers=header)
data = response.text
data_json = json.loads(data)
forecast = data_json["result"]["forecasts"][0]
destrict = data_json["result"]["location"]["name"]
dayWeather = forecast["text_day"]
nightWeather = forecast["text_night"]
high = forecast["high"]
low = forecast["low"]
daywind = forecast["wd_day"]
daywindscale = forecast["wc_day"]
nightwind = forecast["wd_night"]
nightwindscale = forecast["wc_night"]
time0 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
Msg2 = f"{destrict}区明日天气预报:\n明天白天天气:{dayWeather}\n明天夜间天气:{nightWeather}\n气温:最高{high}℃|最低{low}\n白天风力:{daywind}{daywindscale}\n夜间风力:{nightwind}{nightwindscale}\n现在时间是:{time0}"
return Msg2
#爬取明日天气预报
def GetTips():
Tips = ["多喝热水,保持身体水分哦~",
"气温变化极大,请注意防寒保暖~",
"雨天路滑时,请务必注意脚下安全~",
"多雨季节,气候多变,请及时留意窗外,记得带伞~",
"天气干燥,请注意保持水分哦~",
"天气好的时候,放下手中手机,多去户外走走吧~",
"使用手机过久记得适当休息眺望远方哦~"]
Msg3 = random.choice(Tips)
return Msg3
#随机生成提示语
def SendMail(sender,receiver,receiverMail,Msg):
# 实例化 MIMEMultipart 对象赋值给message
message = MIMEMultipart()
# 将发件人信息写入 message["From"]
message["From"] = Header(f"411814431<{sender}>")
# 将收件人信息写入 message["To"]
message["To"] = Header(f"{receiver}<{receiverMail}>")
# 将主题写入 message["Subject"]
message["Subject"] = Header(f"{receiver}你好,这是你的天气预报~")
# 发信内容
mail_content = MIMEText(Msg, "plain", "utf-8")
message.attach(mail_content)
# 使用sendmail(发送人收件人message.as_string())发邮件
smtpObj.sendmail(sender, receiverMail, message.as_string())
# 邮箱帐号、授权码设置
mailUser = input("请输入邮箱账号:")
mailPass = input("请输入邮箱授权码:")
# 使用smtplib.SMTP_SSL(服务器, 端口号)
server = input("请输入邮箱服务器:")
port = input("请输入服务器端口号:")
smtpObj = smtplib.SMTP_SSL(server, port)
# 使用login()函数传入邮箱账户和授权码,登录邮箱
smtpObj.login(mailUser, mailPass)
#receiverDict发件人、收件人
#Receivers接收人行政区代码列表
#两个字典含有的名称需要一致
sender = mailUser
receiverDict = {"徐鑫龙":"411814431@qq.com"}
Receivers = {"徐鑫龙": "430304"}
print(f"正在为设定用户{list(Receivers.keys())}发送天气预报,请耐心等待~")
time.sleep(1)
time2 = 0
for name in Receivers.keys():
print(f"开始给{name}({Receivers[name]})发送天气预报")
time.sleep(1)
Code = Receivers[name]
Msg1 = GetWeatherNow(Code)
Msg2 = GetWeatherForcast(Code)
Msg3 = GetTips()
Messages = f"{Msg1}\n\n{Msg2}\n\n{Msg3}"
SendMail(sender,name,receiverDict[name],Messages)
time2 = time2 + 1
print(f"{name}发送天气预报成功!")
time.sleep(1)
print(f"发送成功,已为{list(Receivers.keys())}发送天气预报~")
print("5秒后窗口自动关闭~")
time.sleep(5)
Loading…
Cancel
Save