刮擦后发送电子邮件 [英] Sending e-mail after scrape in scrapy
问题描述
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')))
在上面的蜘蛛代码中,它将显示蜘蛛的开始时间和结束时间,但是现在蜘蛛完成之后,我想收到来自scrapy的抓取已完成邮件。是否有可能做到这一点。如果可能的话,我们可以使用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 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.
推荐答案
您是否研究过文档:
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屋!