监测小说更新状态发送到邮箱(爬虫和邮件
原创发布在 https://blog.csdn.net/qq_21484935/article/details/103461778
思路:请求小说的url并对内容进行解析,找到带有更新时间的span标签。然后配置邮箱,将内容作为发送。
我选择的是网易的126邮箱,在官网登录账号,设置中,打开“POP3/SMTP/IMAP”,(此处需要手机发送验证消息
设置成功后如图所示:
端口信息如下:
接下来的步骤很简单,python的SMTP操作(不会请百度一下
废话不多说,直接上代码了
import logging
import smtplib
from email.mime.text import MIMEText
import requests
from bs4 import BeautifulSoup
server = 'smtp.126.com'
sender = 'xxxx@126.com' #发送邮箱
mailpass = 'xxxxx' #客户端授权码
receivers =['xxxxx@qq.com'] #接收邮箱
def getUrl(url , header):
req = requests.get(url , header)
bs = BeautifulSoup(req.text , 'html5lib')
time = bs.select('body > div.wrap.cf > div.main > div.chapterlist > div.chapterlist_box > div > div > span')[0]
return time.string #返回更新时间
def send(text):
snd_msg = MIMEText(text,'plain' ,'utf-8')
snd_msg['From'] = "{}".format(sender)
snd_msg['To'] = ",".join(receivers)
snd_msg['Subject'] = '<鬼刀>更新信息'
try:
smtp = smtplib.SMTP_SSL(server, 465) #ssl协议 端口465
smtp.login(sender , mailpass) #发送者邮箱 客户端授权码
smtp.sendmail(sender , receivers , snd_msg.as_string())
print('发送成功!')
except smtplib.SMTPException:
logging.error('failed to send email!')
def init(url):
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
try:
text = getUrl(url , header)
return text
except Exception as e:
print(e)
if __name__ == "__main__":
url = 'http://www.u17.com/comic/68471.html'
text = init(url)
send(text)
后续步骤就不过多说明了。可以设置什么时间去申请,挂载到服务器上,每隔一段时间检测一下更新没有,然后发送到邮箱。是不是很美滋滋。(这个漫画没看过,看多的小伙伴可以吐槽一下呦