Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrapy - Continuously fetch urls to crawl from database

I'd like to continuously fetch urls to crawl from a database. So far I succeeded in fetching urls from the base but I'd like my spider to keep reading from that base since the table will be populated by another thread.

I have a pipeline that removes url from the table once it is crawled (working). In other words, I'd like to use my database as a queue. I tried different approaches with no luck.

Here's my spider.py

class MySpider(scrapy.Spider):
  MAX_RETRY = 10
  logger = logging.getLogger(__name__)

  name = 'myspider'
  start_urls = [
      ]

  @classmethod
  def from_crawler(cls, crawler, *args, **kwargs):
      spider = super(MySpider, cls).from_crawler(crawler, *args, **kwargs)
      crawler.signals.connect(spider.spider_closed, signals.spider_closed)
      return spider

  def __init__(self):
      db = MySQLdb.connect(
          user='myuser',
          passwd='mypassword',
          db='mydatabase',
          host='myhost',
          charset='utf8',
          use_unicode=True
          )
      self.db = db
      self.logger.info('Connection to database opened')
      super(MySpider, self)

  def spider_closed(self, spider):
      self.db.close()
      self.logger.info('Connection to database closed')


  def start_requests(self):
      cursor = self.db.cursor()
      cursor.execute('SELECT * FROM mytable WHERE nbErrors < %s', (self.MAX_RETRY,))
      rows = cursor.fetchall()
        for row in rows:
          yield Request(row[0], self.parse, meta={
              'splash': {
                  'args':{
                      'html': 1,
                      'wait': 2
                      }
                  }
              }, errback=self.errback_httpbin)
      cursor.close()

Thank you very much

EDIT

Here's my new code.

@classmethod
def from_crawler(cls, crawler, *args, **kwargs):
    spider = super(MySpider, cls).from_crawler(crawler, *args, **kwargs)
    crawler.signals.connect(spider.spider_closed, signals.spider_closed)
    crawler.signals.connect(spider.spider_idle, signals.spider_idle)
    return spider

def spider_idle(self, spider):
    self.logger.info('IDLE')
    time.sleep(5)
        for url in self.getUrlsToCrawl():
            self.logger.info(url[1])
            self.crawler.engine.crawl(Request(url[1], self.parse, meta={
               'splash': {
                   'args':{
                       'html': 1,
                       'wait': 5
                       }
                   },
                'dbId': url[0]
               }, errback=self.errback_httpbin), self)
    raise DontCloseSpider       

def getUrlsToCrawl(self):
    dateNowUtc = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
    cursor = self.db.cursor()
    cursor.execute('SELECT id, url FROM mytable WHERE nbErrors < %s AND domain = %s and nextCrawl < %s', (self.MAX_RETRY, self.domain, dateNowUtc))
    urls = cursor.fetchall()
    cursor.close()
    return urls

In my logs I can see :
INFO: IDLE
INFO: someurl
INFO: IDLE
INFO: someurl

But when I update the data in my table to fetch more or less urls, the output never changes. It seems that the data collected is not fresh and I never crawl the requests made in the spider_idle method

like image 671
RogerFromSpace Avatar asked Jan 03 '17 15:01

RogerFromSpace


1 Answers

I would personally recommend to start a new spider every time you have to crawl something but if you want to keep the process alive I would recommend using the spider_idle signal:

@classmethod
def from_crawler(cls, crawler, *args, **kwargs):
    spider = super(MySpider, cls).from_crawler(crawler, *args, **kwargs)
    crawler.signals.connect(spider.spider_closed, signals.spider_closed)
    crawler.signals.connect(spider.spider_idle, signals.spider_idle)
    return spider
...
def spider_idle(self, spider):
    # read database again and send new requests

    # check that sending new requests here is different
    self.crawler.engine.crawl(
                    Request(
                        new_url,
                        callback=self.parse),
                    spider
                )

Here you are sending new requests before the spider actually closes.

like image 51
eLRuLL Avatar answered Oct 21 '22 21:10

eLRuLL