英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论