最近在學習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 </span></div> <div><span style="line-height: 1.5;">總文件數:%s </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> """
實現效果
附Linux下發送html郵件方式:
mail -s "$(echo -e "title\nContent-Type: text/html")" [email protected]