使用方法:
①:python3 get_feiyan.py
生成update.txt 数据
②:python3 get_feiyan.py
发送update.txt 数据
完整代码:
get_feiyan.py
# -*- coding: utf-8 -*-
import json
import re
import time
import requests
url = 'https://3g.dxy.cn/newh5/view/pneumonia'
headers = {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
total = re.search(r'window\.getStatisticsService = ({.*?})', response.text).group(1)
total = json.loads(total)
# 使用time
createTime = time.localtime(float(total['createTime'] / 1000))
modifyTime = time.localtime(float(total['modifyTime'] / 1000))
ruleCreateTime = time.strftime("%Y-%m-%d %H:%M:%S", createTime)
ruleModifyTime = time.strftime("%Y-%m-%d %H:%M:%S", modifyTime)
# print(ruleCreateTime)
print(ruleModifyTime)
data = {'全国': {}}
# data['全国'] = {}
data['全国']['确诊'] = total['confirmedCount']
data['全国']['疑似'] = total['suspectedCount']
data['全国']['死亡'] = total['deadCount']
data['全国']['治愈'] = total['curedCount']
cities = re.findall(r'{"provinceName":.*?]}', response.text)
for city in cities:
city = json.loads(city)
provinceShortName = city['provinceShortName']
confirmedCount = city['confirmedCount']
deadCount = city['deadCount']
curedCount = city['curedCount']
data[provinceShortName] = {}
data[provinceShortName]['确诊'] = confirmedCount
data[provinceShortName]['死亡'] = deadCount
data[provinceShortName]['治愈'] = curedCount
for key in data:
print("%s : %s" % (key, data[key]))
with open("update.txt", "w+", encoding="utf-8") as f:
f.write(ruleModifyTime + "\n")
for key in data:
# print("%s : %s" % (key, data[key]))
f.write("%s : %s" % (key, data[key]) + "\n")
f.write("----------\n")
f.write("\nhttps://ncov.dxy.cn/ncovh5/view/pneumonia")
send_feiyan.py
# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.header import Header
from email.mime.text import MIMEText
# 用于构建邮件头
# 发信方的信息:发信邮箱,QQ 邮箱授权码
from_addr = '1007760854@qq.com'
password = 'QQ邮箱授权码'
# 收信方邮箱
# to_addr = '1007760854,1416495947'
msg_to = ['1007760854', '1416495947']
houzhui = '@qq.com'
for i in range(0, len(msg_to)):
msg_to[i] += houzhui
# 发信服务器
smtp_server = 'smtp.qq.com'
send_msg = ''
with open("update.txt", "r", encoding='utf-8') as f: # 打开文件
data = f.read() # 读取文件
send_msg = data
# 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
msg = MIMEText(send_msg, 'plain', 'utf-8')
# 邮件头信息
msg['From'] = Header(from_addr)
# msg['To'] = Header(to_addr)
msg['To'] = ','.join(msg_to)
msg['Subject'] = Header('肺炎最新情况')
# 开启发信服务,这里使用的是加密传输
server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server, 465)
# 登录发信邮箱
server.login(from_addr, password)
# 发送邮件
# server.sendmail(from_addr, to_addr, msg.as_string())
server.sendmail(from_addr, msg['To'].split(','), msg.as_string().encode("utf-8"))
# 关闭服务器
server.quit()