Skip to content
Advertisement

How can I use scrapy middlewares to call a mail function?

I have 15 spiders and every spider has its own content to send mail. My spiders also have their own spider_closed method which starts the mail sender but all of them same. At some point, the spider count will be 100 and I don’t want to use the same functions again and again. Because of that, I try to use middlewares. I have been trying to use the spider_closed method in middlewares but it doesn’t work.

middlewares.py

JavaScript

settings

JavaScript

I am not getting any error or any mail. I also add some print and there is no output.

How can I run middlewares with my spiders? What is your suggestions?

Advertisement

Answer

It is important to run spider from scrapy crawl command so it will see whole project configuration correctly. Also, you need to make sure that custom middleware is listed in SPIDER_MIDDLEWARES dict and assigned order number. Main entry point for middleware is from_crawler method, which should receive crawler instance. Then you can write your middleware processing logic here by following rules mentioned here.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement