最近在学习Python,这是我第一个Python脚本。没有技术含量,之前不知道zip()函数,想让2个结果对应起来,弄了好久,后来问了做php的同事,才知道。

需求:实现爬虫每次下载完成后生成html统计邮件,发送到邮箱;

#!/usr/bin/env python
#coding:utf8
import os
td = """            <td style="border-style:inset;border-color:#808080;padding:0px;border-width:1px">"""
num = 1
os.system("ls -lth /data0/backup/video/* |awk '{print $1\" \"$3\" \"$NF\" \"$6}'> /tmp/fileinfo.log")
os.system("ls -lth /data0/backup/video/* |awk '{print $5}' > /tmp/filesize.txt")
maketime=os.popen('date +%Y-%m-%d" "%H":"%M').read()
number=os.popen('ls /data0/backup/video/|wc -l').read()
print """
<div><span style="line-height: 1.5;">备份时间:%s&nbsp;</span></div>
<div><span style="line-height: 1.5;">总文件数:%s&nbsp;</span></div>
<div><span style="line-height: 1.5;">文件列表:</span></div>"""%(maketime,number)
print """<table border="1" cellspacing="0px" style="text-align: center; border: 1px outset rgb(128, 128, 128); border-collapse: collapse; width: 60%; border-spacing: 0px;">
<tbody>"""
file=open('/tmp/fileinfo.log')
size=open('/tmp/filesize.txt')
filename=zip(size,file)
print "        <th>编号</th>"
#print "        <th>用户</th>"
print "        <th>时间</th>"
print "        <th>大小</th>"
print "        <th>文件</th>"
for k,v in filename:
info = v.split()
s = k.split()
print "        <tr>"
print  "%s%s</td>"%(td,num)
#print  "%s%s</td>"%(td,info[1])
print  "%s%s</td>"%(td,info[3])
print  "%s%s</td>"%(td,s[0])
print  "%s%s</td>"%(td,info[2])
print "        </tr>"
num = num+1
print """    </tbody>
</table> <div style="text-align: center;"><span style="line-height: 1.7;"></span></div>
"""

 实现效果

Python生成html邮件_统计

 

附Linux下发送html邮件方式:

mail -s "$(echo -e "title\nContent-Type: text/html")" mail@qq.com