Sending email after cleaning

code.py code

class Examplepipeline(object):

def __init__(self):
    dispatcher.connect(self.spider_opened, signal=signals.spider_opened)
    dispatcher.connect(self.spider_closed, signal=signals.spider_closed)

def spider_opened(self, spider):
    log.msg("opened spider  %s at time %s" % (spider.name,datetime.now().strftime('%H-%M-%S')))

def process_item(self, item, spider):
        log.msg("Processsing item " + item['title'], level=log.DEBUG)


def spider_closed(self, spider):
    log.msg("closed spider %s at %s" % (spider.name,datetime.now().strftime('%H-%M-%S')))

The above code of the spider will display the start and end time of the spider, but now, after the spider has finished, I want to get the letter โ€œScraper was completedโ€ from scrapy. Is it possible to do this. If possible, we can write this code in the spider_closed method, can someone please share some sample code on how to do this.

+5
source share
2 answers

Have you looked at the documentation:

http://doc.scrapy.org/en/latest/topics/email.html

The main use of the documentation

from scrapy.mail import MailSender

mailer = MailSender()
mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])

You can also implement something custom yourself. For example, if you want to use gmail:

def send_mail(self, message, title):
    print "Sending mail..........."
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    gmailUser = 'mail_you_send_from@gmail.com'
    gmailPassword = 'password'
    recipient = 'mail_to_send_to'

    msg = MIMEMultipart()
    msg['From'] = gmailUser
    msg['To'] = recipient
    msg['Subject'] = title
    msg.attach(MIMEText(message))

    mailServer = smtplib.SMTP('smtp.gmail.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, gmailPassword)
    mailServer.sendmail(gmailUser, recipient, msg.as_string())
    mailServer.close()
    print "Mail sent"

and just name it like this:

send_mail("some message", "Scraper Report")
+11
source

, yagmail: , Gmail ( , html, ..).

, :

import yagmail
yag = yagmail.SMTP('mail_you_send_from@gmail.com', 'password')

:

yag.send('mail_to_send_to', 'Scraper Report', 'some message')

, , .

( ):

SMTP('mail_you_send_from').send('mail_to_send_to', 'Scraper Report', 'some message')
+1

All Articles