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

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

How to send mail with scraped data?

问题

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

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

items.py

  1. import scrapy
  2. class ScrapeadorItem(scrapy.Item):
  3. Nro = scrapy.Field()
  4. SITUACION = scrapy.Field()

pipelines.py

  1. from itemadapter import ItemAdapter
  2. from scrapy.mail import MailSender
  3. mailer = MailSender(mailfrom="*@gmail.com", smtpuser="*@gmail.com", smtphost="smtp.gmail.com", smtpport=587, smtppass="*")
  4. class ScrapeadorPipeline:
  5. def process_item(self, item, spider):
  6. adapter = ItemAdapter(item)
  7. contenidomail = ""
  8. if adapter.get('SITUACION') == "DESCARGADO":
  9. contenidomail = contenidomail + "\n" + str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
  10. self.contenidomail = contenidomail
  11. return item
  12. def close_spider(self, item):
  13. 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

  1. import scrapy
  2. class ScrapeadorItem(scrapy.Item):
  3. Nro = scrapy.Field()
  4. SITUACION = scrapy.Field()

pipelines.py

  1. from itemadapter import ItemAdapter
  2. from scrapy.mail import MailSender
  3. mailer = MailSender(mailfrom="*@gmail.com", smtpuser="*@gmail.com", smtphost="smtp.gmail.com", smtpport=587, smtppass="*")
  4. class ScrapeadorPipeline:
  5. def process_item(self, item, spider):
  6. adapter = ItemAdapter(item)
  7. contenidomail = ""
  8. if adapter.get('SITUACION') == "DESCARGADO":
  9. contenidomail = contenidomail + "\n" + str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
  10. self.contenidomail = contenidomail
  11. return item
  12. def close_spider(self, item):
  13. mailer.send(to=["*@msn.com"], subject="Reporte", body=self.contenidomail)

答案1

得分: 0

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

  1. class ScrapeadorPipeline:
  2. def __init__(self):
  3. self.contenidomail = ''
  4. def process_item(self, item, spider):
  5. adapter = ItemAdapter(item)
  6. if adapter.get('SITUACION') == "DESCARGADO":
  7. self.contenidomail += str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
  8. return item
  9. def close_spider(self, item):
  10. 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).

  1. class ScrapeadorPipeline:
  2. def __init__(self):
  3. self.contenidomail = ''
  4. def process_item(self, item, spider):
  5. adapter = ItemAdapter(item)
  6. if adapter.get('SITUACION') == "DESCARGADO":
  7. self.contenidomail += str(adapter.get('Nro')) + " " + adapter.get('SITUACION') + "\n"
  8. return item
  9. def close_spider(self, item):
  10. 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:

确定