获取数据之后,如何能把数据写进HTML的代码里,并直接发送给邮箱
import smtplib
from email.mime.text import MIMEText
import os,datetime
import tkinter
import tkinter.messagebox #弹窗库
from datetime import *
def send_miles(file_nei):
mailserver = "smtp.163.com" #邮箱服务器地址
username_send = '*******@163.com' #邮箱用户名
password = 'M*****' #邮箱密码:需要使用授权码
username_recv = ['187**03**@qq.com','******@qq.com'] #收件人,多个收件人用逗号隔开--
mail = MIMEText(file_nei,_subtype='html',_charset='utf-8')
mail['Subject'] = '重要提醒'
mail['From'] = username_send #发件人
mail['To'] = ",".join(username_recv) #收件人;[]里的三个是固定写法,别问为什么,我只是代码的搬运工
smtp = smtplib.SMTP(mailserver,port=25) # 连接邮箱服务器,smtp的端口号是25
# smtp=smtplib.SMTP_SSL('smtp.qq.com',port=465) #QQ邮箱的服务器和端口号
smtp.login(username_send,password) #登录邮箱
smtp.sendmail(username_send,username_recv,mail.as_string())# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print ('success')
def huoqu_html(yuanzu,zhuti):
# d = datetime.now()
# dt = d.strftime('%Y-%m-%d %H:%M:%S')
# at = (d - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S')
html = \
"""
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>"""+zhuti+"""</title>
<body>
<div id="container">
<p><strong>"""+zhuti+"""</strong></p>
<div id="content">
<table width="90%" border="2" bordercolor="red" cellspacing="2" style="border-collapse:collapse">
<tr>
<td><strong>单号</strong></td>
<td><strong>备注名</strong></td>
<td><strong>比例</strong></td>
<td><strong>净值</strong></td>
<td><strong>余额</strong></td>
<td><strong>盈亏</strong></td>
<td><strong>更新时间</strong></td>
</tr>
"""
thbodys=""
for a in yuanzu:
if len(a)>0:
thbody =\
"""
<tr>
<td>""" + str(a[1]) + """</td>
<td>""" + str(a[2]) + """</td>
<td>""" + str(a[4]) + """</td>
<td>""" + str(a[5]) + """</td>
<td>""" + str(a[6]) + """</td>
<td>""" + str(a[7]) + """</td>
<td>""" + str(a[0]) + """</td>
</tr>
"""
thbodys=thbody+thbodys
weiba= \
"""
</table>
</div>
</div>
</div>
</body>
</html>
"""
neirong_html = html+thbodys+weiba
return neirong_html
yuanzu_1=(('da',23,45,58,99,88,988,999,555,2343),('dad',335,77,13,65,77,88,558,4458,99878,558),())
if len(yuanzu_1)>1:
neirong = huoqu_html(yuanzu_1,'特别关注')
send_miles(neirong)
根据这个功能,就可以直接把获取 的数据,发送到邮箱进行储存。