如何使用抓取的数据发送邮件?

huangapple go评论62阅读模式
英文:

How to send mail with scraped data?

问题

如何在Scrapy爬虫结束后发送特定抓取数据的电子邮件。

我正在尝试在爬虫结束时发送一些具有多行数据的项目,但我只收到一行。我认为我需要进行一些迭代,但无法使其工作。

items.py

import scrapy

class ScrapeadorItem(scrapy.Item):
    Nro = scrapy.Field()
    SITUACION = scrapy.Field()

pipelines.py

from itemadapter import ItemAdapter
from scrapy.mail import MailSender

mailer = MailSender(mailfrom="*@gmail.com", smtpuser="*@gmail.com", smtphost="smtp.gmail.com", smtpport=587, smtppass="*")

class ScrapeadorPipeline:
    
    def process_item(self, item, spider):
        
        adapter = ItemAdapter(item)
        contenidomail = ""            
        if adapter.get('SITUACION') == "DESCARGADO":
            contenidomail = contenidomail + "\n" + str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
            self.contenidomail = contenidomail
    
        return item
    
    def close_spider(self, item):
        mailer.send(to=["*@msn.com"], subject="Reporte", body=self.contenidomail)
英文:

How to send email with specific scraped data after spider ends in Scrapy.

I'm trying to send some item data with multiple lines after the end of a spider, but i'm only receiving one line. I think i have to do some iteration but i can't make it work.

items.py

import scrapy

class ScrapeadorItem(scrapy.Item):
    Nro = scrapy.Field()
    SITUACION = scrapy.Field()

pipelines.py

from itemadapter import ItemAdapter
from scrapy.mail import MailSender

mailer = MailSender(mailfrom="*@gmail.com", smtpuser="*@gmail.com", smtphost="smtp.gmail.com", smtpport=587, smtppass="*")

class ScrapeadorPipeline:
    
    def process_item(self, item, spider):
        
        adapter = ItemAdapter(item)
        contenidomail = ""            
        if adapter.get('SITUACION') == "DESCARGADO":
            contenidomail = contenidomail + "\n" + str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
            self.contenidomail = contenidomail
    
        return item
    
    def close_spider(self, item):
        mailer.send(to=["*@msn.com"], subject="Reporte", body=self.contenidomail)

答案1

得分: 0

你应该将每个项目连接到 self.contenidomail,而不是本地的 contenidomail 变量(你刚刚初始化为空)。

class ScrapeadorPipeline:

    def __init__(self):
        self.contenidomail = ''

    def process_item(self, item, spider):
        adapter = ItemAdapter(item)
        if adapter.get('SITUACION') == "DESCARGADO":
            self.contenidomail += str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
        return item

    def close_spider(self, item):
        mailer.send(to=["*@msn.com"], subject="Reporte", body=self.contenidomail)
英文:

You should concatenate each item to self.contenidomail, not the local contenidomail variable (which you just initialized to be empty).

class ScrapeadorPipeline:

    def __init__(self):
        self.contenidomail = ''
    
    def process_item(self, item, spider): 
        adapter = ItemAdapter(item)
        if adapter.get('SITUACION') == "DESCARGADO":
            self.contenidomail += str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"    
        return item
    
    def close_spider(self, item):
        mailer.send(to=["*@msn.com"], subject="Reporte", body=self.contenidomail)

huangapple
  • 本文由 发表于 2023年6月2日 08:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定