Python的scrapy框架爬虫项目中加入邮箱通知(爬虫启动关闭等信息以邮件的方式发送到邮箱)

前面关于发送邮件的博客参考:普通邮件博客——点击打开链接

                                             带附件的邮件——点击打开链接

准备:
1、创建scrapy爬虫项目

2、代码主要是两部分:

呈上代码
第一部分是发送邮件的代码:

import smtplib
from email.mime.text import MIMEText
 
import logging
class EmailSend(object):
    def __init__(self):
        self.logging = logging.getLogger('Waring')
        self.email_host = 'smtp.qq.com'
        self.email_port = '465'
        self.email_pass = '授权码(或密码,不推荐密码)'
 
    def send_text_email(self, from_addr, to_addrs, subject, content):
        self.logging.warning('send_text_email is willed 丢弃')
        self.logging.error('send_text_email is None')
        message_text = MIMEText(content, 'plain', 'utf8')
        message_text['From'] = from_addr
        message_text['To'] = to_addrs
        message_text['Subject'] = subject
 
        try:
            # 在创建客户端对象的同时,连接到邮箱服务器。
            client = smtplib.SMTP_SSL(host=self.email_host, port=self.email_port)
            login_result = client.login(from_addr, self.email_pass)
            if login_result and login_result[0] == 235:
                print('登录成功')
                client.sendmail(from_addr, to_addrs, message_text.as_string())
                print('邮件发送成功')
            else:
                print('邮件发送异常:',login_result[0], login_result[1])
        except Exception as e:
            # print('连接邮箱服务器异常:',e)
            self.logging.error('连接邮箱服务器异常:{}'.format(e))
    def send_image_email(self):
        pass
 
    def send_word_email(self):
        pass
 
    def send_video_email(self):
        pass
 
"""
1. start_spider 
"""
 
 
注意:上述代码中可自定义发送内容(文本:send_word_email,视频:send_video_email....)主要是在爬虫项目中调用该模块发送邮件(包含参数:

from_addr, to_addrs, subject, content
)

爬虫项目代码如下:

# -*- coding: utf-8 -*-
import scrapy, time
from datetime import datetime
from ..emailsend import EmailSend
 
class EmailtestSpider(scrapy.Spider):
    name = 'emailtest'
    allowed_domains = ['baidu.com']
    start_urls = ['http://www.baidu.com/']
 
    # 在爬虫启动和关闭的时候,分别发送邮箱,通知爬虫管理者。
    def start_requests(self):
        email = EmailSend()
        content = '爬虫启动时间:{}'.format(datetime.now())
        email.send_text_email('发送邮箱@qq.com', '接收邮箱@qq.com', '爬虫结束', content)
 
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse)
 
    def parse(self, response):
        time.sleep(10)
        print('123')
 
    def closed(self, reason):
        # 爬虫关闭的时候,会调用这个方法
        email = EmailSend()
        content = '爬虫关闭时间:{}'.format(datetime.now())
        email.send_text_email('发送邮箱@qq.com', '接收邮箱@qq.com', '爬虫结束', content)

注意:1、示例代码中邮件内容是当前时间(爬虫开始结束时间:作用可以计算爬虫速度!),当发送消息是需要调用,要注意发送的参数是:
‘发送邮箱@qq.com’, ‘接收邮箱@qq.com’, ‘爬虫结束’, content

    2、最后爬虫结束是的closed函数

closed函数对应的是爬虫项目中(源码)的close函数,如下:


原文:https://blog.csdn.net/qq_33472765/article/details/81022979
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_43061705/article/details/83794858
今日推荐