import urllib.request
import re
import time
import smtplib
import calendar
import formatter
from datetime import date
from 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
#获取输入的网页单页html
def 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('‘',"'")
topicurl = topicurl.replace('’',"'")
topicurl = topicurl.replace('·',"*")
topicurl = topicurl.replace('—',"-")
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('采集完成!')
磨铁追书2.0版,实现邮件发送附带链接
最新推荐文章于 2022-05-29 12:51:50 发布