import datetime import requests import json import smtplib from email.header import Header from email.mime.text import MIMEText mail_host = "smtp.qq.com" # SMTP服务器 mail_user = "qwqdanchun@qq.com" # 用户名 mail_pass = "lcslxfovezymbhbj" # 授权密码,非登录密码 sender = 'qwqdanchun@qq.com' receivers = [ 'qwqdanchun@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 content = '宿舍电量不足5度,请及时充值。from簞純' title = '宿舍电量不足' # 邮件主题 def sendEmail(): message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码 message['From'] = "{}".format(sender) message['To'] = ",".join(receivers) message['Subject'] = title try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("mail has been send successfully.") except smtplib.SMTPException as e: print(e) def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content): email_client = smtplib.SMTP(SMTP_host) email_client.login(from_account, from_passwd) # create msg msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') # subject msg['From'] = from_account msg['To'] = to_account email_client.sendmail(from_account, to_account, msg.as_string()) email_client.quit() def get_html(): url = 'http://www.stuzf.sdu.edu.cn/wxapp/api/pay/queryElectricity' headers = { 'Host': 'www.stuzf.sdu.edu.cn', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Linux; Android 9; MI 6X Build/PKQ1.180904.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/6.2 TBS/044813 Mobile Safari/537.36 MMWEBID/7279 MicroMessenger/7.0.6.1460(0x27000634) Process/tools NetType/WIFI Language/zh_CN', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Referer': 'http://www.stuzf.sdu.edu.cn/wxapp/api/pay/queryElectricity', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cookie': 'JSESSIONID=9861c52f-28f3-4f97-958d-4dfd9f0ccf2d', 'X-Requested-With': 'XMLHttpRequest', 'Content - Type': 'application / x - www - form - urlencoded;charset = UTF - 8' } data = { 'openId': 'oNMvKvkcpwYYqN6QLSijsVamwnBU', 'userXq': '兴隆山校区', 'userFj': '1302032', } html = requests.post(url,headers = headers,data=data).text print(html) jsonobj = json.loads(html.encode('utf-8').decode('utf-8')) houhouhou = jsonobj['message']['plusElec'] print(houhouhou) dianliang = float(houhouhou) print(dianliang) if (dianliang>=5.0): print('电量充足') else: print('电量不足') sendEmail() get_html()