实现磨铁追书2.0版,磨铁书2.0,上一次实现了简单的追书功


上一次实现了简单的追书功能,这次花时间实现了邮件发送并且附带链接

实现方式比较笨,因为不知道如何匹配完整链接+章节名称,所以正则匹配了两次,发送的邮件格式也非常难看

说实话,因为搞不定登录,所以目前附带链接发邮件,自己手工登录吧

看来几年不写代码,退化的不是一般的厉害

代码在python3.3下执行通过,环境是mac os 10.9,其他操作系统环境应该也问题不大,不过只支持python3

import urllib.requestimport reimport timeimport smtplibimport calendarimport formatterfrom datetime import datefrom email.mime.text import MIMEText#要发给谁,可以发给多个人mailto_list=["aaa@gmail.com",""]######################设置服务器,用户名、口令以及邮箱的后缀mail_host="smtp.163.com"mail_user="aaa"mail_pass="aaa"mail_postfix="163.com"######################def send_mail(to_list,sub,content):    #to_list:发给谁    #sub:主题    #content:内容    #send_mail("aaa@126.com","sub","content")    me = "aaa <aaa@163.com>"    msg = MIMEText(content)    msg = MIMEText(content,'html','utf-8')    msg['Subject'] = sub    msg['From'] = me    msg['To'] = ";".join(mailto_list)    try:        s = smtplib.SMTP()        s.connect(mail_host)        s.login(mail_user,mail_pass)        s.sendmail(me, mailto_list, msg.as_string())        s.close()        return True    except Exception as e:        print(str(e))        return False#获取输入的网页单页htmldef getHtml2(url2):    html2=urllib.request.urlopen(url2).read().decode('utf-8')    return html2#抽取章节相关列表def gettopic(html2):    reg3=r"第[^<]+<"    createdate = r"createdate[^>]+>"    linksList = re.findall('<a href="(.*?)" >第.*?</a>',html2)    #for link in linksList:    #   tt= ''    #   tt = 'http://www.motie.com' + link    #   print(tt)    #    #kk = len(linksList)    #if kk > 0:    #   for i in range(1,5):    #       tt = 'http://www.motie.com' + linksList[kk-i]    #       #print(tt)    #print(kk)    cdlist = re.findall(createdate,html2)    topiclist3=re.findall(reg3,html2)    l = len(cdlist)    ll = cdlist[l-1]    kk = len(ll)    ll = ll[12:kk-2]    #得到最新更新时间    #print(ll)    x = 0    y = ''    z = 0    j = ''    #读取章节列表    #print(topiclist3)    for topicurl in topiclist3:        #替换一些很无聊的不识别字符以及标题中的没用符号        topicurl = topicurl.replace('&lsquo;',"'")        topicurl = topicurl.replace('&rsquo;',"'")        topicurl = topicurl.replace('&middot;',"*")        topicurl = topicurl.replace('&mdash;',"-")        topicurl = topicurl.replace('<','')        topicurl = topicurl.replace('。','')        topicurl = topicurl.replace('、','')        x+=1        if (topicurl.find('卷')!=-1):            z+=1            j = j + topicurl + ','            y = y + '\\n\\n' + topicurl + '\\n\\n'        else:            topicurl = topicurl.ljust(30)            y = y + topicurl + 'http://www.motie.com' + linksList[x-z-1] + '\\n'    j = j[:len(j)-1]     #打印章节    print(y)    #print(j)    print()    print("章节数目: %d"%(x-z))    print("分卷数目: %d"%z)    print("最新章节: %s"%topicurl)    print("更新时间: %s"%ll)    print("今天日期: %s"%date.today())    print()    yy = ll[:10]    #if send_mail(mailto_list,'屌丝道士最新章节: '+topicurl+',更新时间:'+ll,y):    #   print('发送成功')    #else:    #   print('发送失败')    if yy==date.today():        print("今天有更新")        if send_mail(mailto_list,'屌丝道士最新章节: '+topicurl+',更新时间:'+ll,y):            print('发送成功')        else:            print('发送失败')    else:        print("今天无更新")        print("最新更新时间为:"+ll)    return topicurl#调用函数,读取小说章节目录html2=getHtml2('http://www.motie.com/book/24336/chapter')topicurl=gettopic(html2)print('采集完成!')#该片段来自于http://byrx.net

评论关闭