1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import smtplib import time from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
import requests import re from loguru import logger
shelf_id = "13163494"
delay_time = 60
before_status = ""
logger.add("./log.txt", format="{time} {level} {message}", level="INFO")
def get_shelf_status(): response = requests.get(f"https://www.zuhaowan.com/zuhao/{shelf_id}.html") response = response.text result = re.findall("haoZtMap: \".*?\"", response) if len(result) != 1: logger.error("关键词没找到,无法判断货架状态") return "" result_list = result[0].split("\"") status_string = result_list[1] logger.info(f"关键词已找到: {status_string}") return status_string
def send_email(content): logger.debug("开始发送邮件提醒")
host = "smtp.qq.com" port = 465 login_email = "[email protected]" login_password = "xxxxxxxxxxxxxxxx" send_from = "[email protected]" send_to = "[email protected]" email_title = "租号玩订单状态提醒" email_content = content
con = smtplib.SMTP_SSL(host, port)
con.login(login_email, login_password)
msg = MIMEMultipart() msg["Subject"] = Header(email_title, "utf-8").encode() msg["From"] = f"{send_from} <{send_from}>" msg["To"] = send_to
msg.attach(MIMEText(email_content, "plain", "utf-8"))
con.sendmail(send_from, send_to, msg.as_string())
con.quit()
logger.info("完成发送邮件提醒")
if __name__ == '__main__': while True: current_status = get_shelf_status() if current_status != before_status: send_email(current_status) before_status = current_status time.sleep(delay_time)
|