刮擦后发送电子邮件 [英] Sending e-mail after scrape in scrapy

查看:123
本文介绍了刮擦后发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pipeline.py代码

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

在上面的蜘蛛代码中,显示蜘蛛的开始时间和结束时间,但是现在蜘蛛完成之后,我想要收到一个邮件,刮完已经完成了。是否可以这样做?如果可能的话我们可以用spider_closed方法编写该代码,任何人都可以分享一些关于如何做这个的示例代码。

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 recieve 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.

感谢advacne ..........

Thanks in advacne..........

推荐答案

您是否查阅了文档:

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

文件的基本用法

from scrapy.mail import MailSender

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

此外,您还可以自行实现自定义。例如,如果要使用gmail:

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 = '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"

,只需调用:

send_mail("some message", "Scraper Report")

这篇关于刮擦后发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆