Ramesh KC
Ramesh KC

Reputation: 582

Scrapy email sending once at a time when spider runs

I am trying to send the email in gmail when spider finished to scraping the page..when I define function send_mail and pass it like below ,in log,it says send_mail("some message", "Scraper Report") NameError: name 'send_mail' is not defined ..how can I send the gmail when spider finished scraping. when I passed the send_mail function inside def parse(self,response) method it try to block my gmail,due to looping of scraping..

 class ekantipurSpider(XMLFeedSpider):
    name= "ekantipur"
    allowed_domains = ["ekantipur.com"]
    start_urls = [
    'http://www.ekantipur.com/archive/'

  ]


send_mail("some message", "Scraper Report")

        def parse(self, response):

          hxs = HtmlXPathSelector(response) # The xPath selector
          titles=hxs.select('//div[@id = "archive-content-wrapper"]//ul/li')
          items = []
          for titles in titles:
           item = NewsItem()
           item['title']=titles.select('h6/a/text()').extract()[0]
           item['link']=titles.select('h6/a/@href').extract()[0]
           item['description']=titles.select('p/text()').extract()[0]
           item = Request(item['link'],meta={'item':item},callback=self.parse_detail)
           items.append(item)



        return items


     def parse_detail(self,response):
      item = response.meta['item']
      sel = HtmlXPathSelector(response)
      detail = sel.select('//div[@class = "main_wrapper_left"]')
      item['details'] = escape(''.join(detail.select('p/text()').extract()))
      locationDate = item['details'].split(':',1)[0]
      item['location']= locationDate.split(",",1)[0]
      item['published_date'] =    escape(''.join(detail.select('p[last()]/text()').extract()))


      return item


def send_mail(self, message, title):
  print "Sending mail..........."
  gmailUser = '[email protected]'
  gmailPassword = 'rameshkc8  '
  recipient = '[email protected]'

 msg = MIMEMultipart()
 msg['From'] = gmailUser
 msg['To'] = recipient
 msg['Subject'] = title
 msg.attach(MIMEText(message))

 mailServer = smtplib.SMTP('smtp.gmail.com', 587)
 mailServer.ehlo()
 mailServer.starttls()
 mailServer.ehlo()
 mailServer.login(gmailUser, gmailPassword)
 mailServer.sendmail(gmailUser, recipient, msg.as_string())
 mailServer.close()
 print "Mail sent"

Upvotes: 0

Views: 404

Answers (1)

Bee Smears
Bee Smears

Reputation: 803

you need to add self. to send mail (self.send_mail(args)), if they're in the same class. if they're not, then you need to call send_mail() after the function is defined.

Upvotes: 1

Related Questions