python 判断页面正常报警

作者: admin 分类: python 发布时间: 2012-08-25 11:28 ė 6 没有评论
<pre># coding=utf8
import socket
import urllib2,re
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mailto_list=["12345678912@139.com","123456789123@139.com"]
mail_host="mail.linuxqq.com"
mail_user="webmaster@linuxqq.com"
mail_pass="test"
mail_postfix="linuxqq.com"

class webspider(object):
        def __init__(self,url):
                self.webdata=urllib2.urlopen(url,timeout = 5).read().lower()
                p=re.compile("\s+")
                self.new_webdata=re.sub(p,'',self.webdata)

        def send_mail(self,to_list,sub,content):
                me=mail_user + "<" + mail_user +  ">"
                msg=MIMEText(content)
                msg['Subject'] = sub
                msg['From'] = me
                msg['To'] = ";".join(to_list)
                try:
                        s = smtplib.SMTP()
                        s.connect(mail_host)
                        s.login(mail_user,mail_pass)
                        s.sendmail(me, to_list, msg.as_string())
                        s.close()
                        return True
                except Exception, e:
                        print str(e)
                        return False

        def get_title(self):
                ptitle = "<title>.*</title>"
                title=re.search(ptitle,self.new_webdata)
                if title == None:
                        if self.send_mail(mailto_list,"TAGS error","Note:tags empty!!!!!!error!!!error!!!!"):
                                print "发送成功"


if __name__=='__main__':
        url = "http://www.linuxqq.com/index.php"
        web = webspider(url)
        web.get_title()
</pre>

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/886.html

0
更多
Ɣ回顶部