2013-06-08 4 views
6

Ho uno script chiamato algorithm.py e voglio essere in grado di chiamare gli spider Scrapy durante lo script. Il file è scructure:Come chiamare particolari spider Scrapy da un altro script Python

algorithm.py MySpiders/

dove MySpiders è una cartella contenente diversi progetti Scrapy. Mi piacerebbe creare metodi perform_spider1(), perform_spider2() ... che posso chiamare in algorithm.py.

Come si costruisce questo metodo?

Sono riuscito a chiamare uno spider utilizzando il seguente codice, tuttavia, non è un metodo e funziona solo per uno spider. Sono un principiante che ha bisogno di aiuto!

import sys,os.path 
sys.path.append('path to spider1/spider1') 
from twisted.internet import reactor 
from scrapy.crawler import Crawler 
from scrapy.settings import Settings 
from scrapy import log, signals 
from scrapy.xlib.pydispatch import dispatcher 
from spider1.spiders.spider1_spider import Spider1Spider 

def stop_reactor(): 
    reactor.stop() 

dispatcher.connect(stop_reactor, signal=signals.spider_closed) 

spider = RaListSpider() 
crawler = Crawler(Settings()) 
crawler.configure() 
crawler.crawl(spider) 
crawler.start() 
log.start() 
log.msg('Running reactor...') 
reactor.run() # the script will block here 
log.msg('Reactor stopped.') 

risposta

5

Basta passare attraverso i vostri ragni e li istituito tramite chiamando configure, crawl e start, e solo allora chiamano log.start() e reactor.run(). E scrapy eseguirà più spider nello stesso processo.

Per ulteriori informazioni, vedere documentation e this thread.

Inoltre, considera l'esecuzione dei tuoi spider tramite scrapyd.

Spero che questo aiuti.

+0

Grazie, alecxe! Come posso fermare il reattore dopo l'ultimo ragno? Attualmente sto usando def stop_reactor(): reactor.stop() dispatcher.connect (stop_reactor, segnale = signals.spider_closed) Tuttavia, questo si ferma dopo il primo ragno ... –

+0

Sei il benvenuto. Buona domanda! Che ne dici di tenere traccia degli spider chiusi in 'stop_reactor 'manualmente e fermare il reattore se tutti sono stati chiusi? A proposito, ho modificato la risposta e incluso il link a una discussione pertinente. – alecxe

+0

Grazie, amico. Non ho abbastanza reputazione per potermi votare, ma io, moralmente, voterò in su invece :) –

2

Sulla base del buon consiglio di Alecx, ecco una possibile soluzione.

import sys,os.path 
sys.path.append('/path/ra_list/') 
sys.path.append('/path/ra_event/') 
from twisted.internet import reactor 
from scrapy.crawler import Crawler 
from scrapy.settings import Settings 
from scrapy import log, signals 
from scrapy.xlib.pydispatch import dispatcher 
from ra_list.spiders.ra_list_spider import RaListSpider 
from ra_event.spiders.ra_event_spider import RaEventSpider 

spider_count = 0 
number_of_spiders = 2 

def stop_reactor_after_all_spiders(): 
    global spider_count 
    spider_count = spider_count + 1 
    if spider_count == number_of_spiders: 
     reactor.stop() 


dispatcher.connect(stop_reactor_after_all_spiders, signal=signals.spider_closed) 

def crawl_resident_advisor(): 

    global spider_count 
    spider_count = 0 

    crawler = Crawler(Settings()) 
    crawler.configure() 
    crawler.crawl(RaListSpider()) 
    crawler.start() 

    crawler = Crawler(Settings()) 
    crawler.configure() 
    crawler.crawl(RaEventSpider()) 
    crawler.start() 

    log.start() 
    log.msg('Running in reactor...') 
    reactor.run() # the script will block here 
    log.msg('Reactor stopped.')