天天看点

发送邮件和scrapy结合

# -*- 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('[email protected]', '[email protected]', '爬虫启动', 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('[email protected]', '[email protected]', '爬虫结束', content)
           
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 = 'zsgncmacktxkbdhb'

    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
           
# LOG_FILE: 配置收集日志的本地目录
# LOG_ENABLED: 配置是否启用日志收集
# LOG_ENCODING
# LOG_LEVEL: 配置收集日志的级别,INFO(含有及以上),ERROR(包含以以上)
# LOG_FORMAT
# LOG_DATEFORMAT
# LOG_STDOUT
# LOG_SHORT_NAMES

LOG_FILE = 'mylog.log'
LOG_LEVEL = 'WARNING'
           

文件结构:

发送邮件和scrapy结合