【代码】Python3爬虫爬取租号玩订单状态并发送邮件

前言

Python3爬虫爬取租号玩订单状态并发送邮件

源代码

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
# print(response)
# 通过关键词获取结果
result = re.findall("haoZtMap: \".*?\"", response)
# print(result)
if len(result) != 1:
logger.error("关键词没找到,无法判断货架状态")
return ""
# 字符串分割
result_list = result[0].split("\"")
# print(result_list)
# 获取状态
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)

完成

参考文献

CSDN——酒坛坛儿^_^