Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending e-mail after scrape in scrapy

pipeline.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')))

In the above spider code , it will display the starting time and ending time of the spider, but now after the completion of the spider, i want to receive a mail that "Scraping has been completed" from scrapy. Is it possible to do this. If possible can we write that code in spider_closed method, can anyone please share some example code on how to do this.

like image 778
Shiva Krishna Bavandla Avatar asked Jul 10 '12 10:07

Shiva Krishna Bavandla


2 Answers

Have you looked into documentation:

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

Basic usage from documentation

from scrapy.mail import MailSender

mailer = MailSender()
mailer.send(to=["[email protected]"], subject="Some subject", body="Some body", cc=["[email protected]"])

Also you could implement something custom on your own. 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 = '[email protected]'
    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 call it like:

send_mail("some message", "Scraper Report")
like image 192
iblazevic Avatar answered Oct 30 '22 20:10

iblazevic


My apologies for self-promotion, but I recently created yagmail: a package that strives to make it easy to send gmail messages (text, html, image etc).

This is the code you'd need to connect:

import yagmail
yag = yagmail.SMTP('[email protected]', 'password')

Then you use this to send emails:

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

What is nice is that you do not have to keep settings around as text, but you can rely on the OS' keyring for a real safe and comfortable feeling.

It could even be a one liner (that closes automatically):

SMTP('mail_you_send_from').send('mail_to_send_to', 'Scraper Report', 'some message')
like image 27
PascalVKooten Avatar answered Oct 30 '22 19:10

PascalVKooten