Scrapy 使用扩展发送包含已删除数据的电子邮件
需要帮助 在抓取站点并通过管道处理数据后,我需要通过电子邮件发送抓取的数据。我试着阅读了所有的东西,但似乎无法把这些点联系起来。 在管道中,我尝试了以下方法:Scrapy 使用扩展发送包含已删除数据的电子邮件,scrapy,Scrapy,需要帮助 在抓取站点并通过管道处理数据后,我需要通过电子邮件发送抓取的数据。我试着阅读了所有的东西,但似乎无法把这些点联系起来。 在管道中,我尝试了以下方法: 类电子邮件管道(对象): def关闭_卡盘(自身,卡盘): 从_电子邮件=”myemail@email.com" 发送电子邮件=”anotheremail@email.com" msg=MIMEMultipart() msg['From']=来自您的电子邮件 msg['To']=发送电子邮件 msg['Subject']='scraper
类电子邮件管道(对象):
def关闭_卡盘(自身,卡盘):
从_电子邮件=”myemail@email.com"
发送电子邮件=”anotheremail@email.com"
msg=MIMEMultipart()
msg['From']=来自您的电子邮件
msg['To']=发送电子邮件
msg['Subject']='scraper Results'
intro=“Scrapy spider的摘要统计信息:\n\n”
body=spider.crawler.stats.get_stats()
正文=打印格式(正文)
body=intro+body
msg.attach(MIMEText(正文“普通”))
server=smtplib.SMTP(“mailserver”,465)
server.startssl()
服务器登录(“用户”、“密码”)
text=msg.as_string()
server.sendmail(从电子邮件到电子邮件,文本)
server.quit()
我应该通过管道或扩展发送电子邮件,还是首选?我将如何实现它
谢谢大家 这是一个文件,您可以使用并导入此发送邮件功能。你将需要改变一些事情,以使其适合你的情况。通过管道以正确的方式将其包括在内
import smtplib
# For guessing MIME type
import mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# Import the email modules we'll need
import email
def send_mail(filename):
sender = 'sender@email.com'
reciever = 'receiver@email.com'
marker = "AUNIQUEMARKER"
msg = MIMEMultipart()
msg['Subject'] = 'Subject text here'
msg['From'] = sender
msg['To'] = reciever
# Read a file and encode it into base64 format
fo = open(filename, "rb")
att = MIMEApplication(fo.read(),_subtype="pdf")
msg.attach(att)
fo.close()
try:
smtpObj = smtplib.SMTP(host='smtp.host.com', port=587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(sender, 'your password')
smtpObj.sendmail(sender, reciever, msg.as_string())
print('SUCCESSFULLY SENT EMAIL')
return
except Exception as e:
print("SEND E-MAIL FAILED WITH EXCEPTION: {}".format(e))
return
在输出目录中查找最后修改的文件的另一个步骤
import os
import glob
download_dir = "/full/path/to/files/"
def get_newest_file():
print("Finding latest pdf file")
file_list = glob.glob('{}*.pdf'.format(download_dir))
latest_file = max(file_list, key=os.path.getctime)
if latest_file:
print("Latest file: {}".format(latest_file))
return latest_file
Scrapy提供模块(基于smtplib
):
你会抓取许多项目并将其保存为文件,然后将该文件作为附件发送吗?如果你正在抓取大量数据,然后通过电子邮件发送电子表格,则需要提供完整的路径,以便我的答案能够正常工作,那么你使用spider close方法是正确的。您只需要创建一个输出目录和一个函数来查找最后修改的文件。我会更新答案。。
from scrapy.mail import MailSender
mailer = MailSender()
mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])