下载网页,发邮件到邮箱

来源:互联网 发布:霍先生加的安之知TXT 编辑:程序博客网 时间:2024/04/28 10:25
<pre name="code" class="python"># -*- coding: utf-8 -*-import urllib2import osimport datetimeimport smtplibfrom email.mime.text import MIMETextmailto_list=["*******@qq.com"]#####################mail_host="smtp.126.com"mail_user="name"mail_pass="****"mail_postfix="126.com"######################def get_httpcode(url,id):    headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}        try:        response=urllib2.Request(url,headers=headers)        res=urllib2.urlopen(response).code        html=urllib2.urlopen(response).read()        if res == 200:            print "Begin to download the page "            if os.path.exists('D:\\Program\\html\\'+id+'.html')==True:                print 'The file is exist'            else:                print 'Download the page now'                    file=open('D:\\Program\\html\\'+id+'.html','w')                file.write(html)                file.close()                send_mail(mailto_list,"subject",url)                print 'Download complete'    except  urllib2.HTTPError as e:        if e.code==404:            print 'Error 404'        else :            print e.code            def send_mail(to_list,sub,content):         me=mail_user+"<"+mail_user+"@"+mail_postfix+">"        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        if __name__=='__main__':    #num=38727380    starttime=datetime.datetime.now()    headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}    for i in range(38727389,38830000):        url='http://blog.csdn.net/****/article/details/' + str(i)        print url        list1=url[-8:]        get_httpcode(url,list1)            endtime=datetime.datetime.now()    usetime=endtime-starttime    print 'The Program cost '+str(usetime)               


                                             
0 0
原创粉丝点击