【问题标题】:Scrapy email sending once at a time when spider runs蜘蛛运行时一次发送一次垃圾邮件
【发布时间】:2014-08-08 08:41:34
【问题描述】:

当蜘蛛完成抓取页面时,我正在尝试在 gmail 中发送电子邮件..当我定义函数 send_mail 并像下面一样传递它时,在日志中,它显示 send_mail("some message", "Scraper Report") NameError: name 'send_mail' is not defined ..蜘蛛完成抓取后如何发送 gmail。当我在 def parse(self,response) 方法中传递 send_mail 函数时,由于抓取循环,它试图阻止我的 gmail..

 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 = 'manthali2014@gmail.com'
  gmailPassword = 'rameshkc8  '
  recipient = 'kcramesh8@gmail.com'

 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"

【问题讨论】:

  • defn 和 call 中的参数不匹配

标签: python email scrapy scrapy-spider


【解决方案1】:

如果他们在同一个班级,您需要添加self. 来发送邮件 (self.send_mail(args))。如果不是,那么您需要在定义函数后调用 send_mail() 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2012-03-24
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    相关资源
    最近更新 更多