# -*- 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'
檔案結構:
