2011-06-27 22 views
7

मैं here पर चर्चा के रूप में एक स्क्रिप्ट से स्केपर चलाने की कोशिश कर रहा हूं। इसने this स्निपेट का उपयोग करने का सुझाव दिया, लेकिन जब मैं ऐसा करता हूं तो यह अनिश्चित काल तक लटकता है। यह संस्करण 10 में वापस लिखा गया था; क्या यह अभी भी स्थिर स्थिर के साथ संगत है?एक स्क्रिप्ट से स्कैनिंग चल रहा है - हैंग

+0

:

for spiderConfig in spiderConfigs: spiderConfig = spiderConfig.copy() # a dictionary similar to the one with global settings above spiderName = spiderConfig.pop('name') # name of the spider is in the configs - i can use the same spider in several instances - giving them different names spiderModuleName = spiderConfig.pop('spiderClass') # module with the spider is in the settings spiderModule = __import__(spiderModuleName, {}, {}, ['']) # import that module SpiderClass = spiderModule.Spider # spider class is named 'Spider' spider = SpiderClass(name = spiderName, **spiderConfig) # create the spider with given particular settings crawlerProcess.queue.append_spider(spider) # add the spider to spider pool 

मकड़ियों के लिए फ़ाइल में सेटिंग का उदाहरण यह प्रश्न और उत्तर अद्यतन के लिए तैयार हो सकता है। यहां [स्केपर से हालिया स्निपेट] है (http://scrapy.readthedocs.org/en/0.16/topics/practices.html)। यह काम करता है, लेकिन मेरे लिए सवाल यह हो जाता है: आप ट्विस्ट रिएक्टर को कैसे रोकते हैं और कब किया जाता है? – bahmait

उत्तर

7
from scrapy import signals, log 
from scrapy.xlib.pydispatch import dispatcher 
from scrapy.crawler import CrawlerProcess 
from scrapy.conf import settings 
from scrapy.http import Request 

def handleSpiderIdle(spider): 
    '''Handle spider idle event.''' # http://doc.scrapy.org/topics/signals.html#spider-idle 
    print '\nSpider idle: %s. Restarting it... ' % spider.name 
    for url in spider.start_urls: # reschedule start urls 
     spider.crawler.engine.crawl(Request(url, dont_filter=True), spider) 

mySettings = {'LOG_ENABLED': True, 'ITEM_PIPELINES': 'mybot.pipeline.validate.ValidateMyItem'} # global settings http://doc.scrapy.org/topics/settings.html 

settings.overrides.update(mySettings) 

crawlerProcess = CrawlerProcess(settings) 
crawlerProcess.install() 
crawlerProcess.configure() 

class MySpider(BaseSpider): 
    start_urls = ['http://site_to_scrape'] 
    def parse(self, response): 
     yield item 

spider = MySpider() # create a spider ourselves 
crawlerProcess.queue.append_spider(spider) # add it to spiders pool 

dispatcher.connect(handleSpiderIdle, signals.spider_idle) # use this if you need to handle idle event (restart spider?) 

log.start() # depends on LOG_ENABLED 
print "Starting crawler." 
crawlerProcess.start() 
print "Crawler stopped." 

अद्यतन:

आप भी की आवश्यकता है मकड़ी प्रति सेटिंग्स इस उदाहरण देखें:

name = punderhere_com  
allowed_domains = plunderhere.com 
spiderClass = scraper.spiders.plunderhere_com 
start_urls = http://www.plunderhere.com/categories.php? 
+0

मुझे यह [https://gist.github.com/1051117) ट्रेसबैक मिलता है। मेरी स्केपर परियोजना को स्क्रैपर नाम दिया गया है। क्या यह समस्या हो सकती है? – ciferkey

+0

मुझे लगता है कि यह मुद्दा है। यह एक असली परियोजना से है। आप स्क्रैपर के संदर्भ हटा सकते हैं। आपको बस मकड़ियों के लिए कुछ सेटिंग्स की जरूरत है। – warvariuc

+0

तो जब मैं अपने प्रोजेक्ट के लिए सेटिंग्स आयात करने के बारे में कैसे जा सकता हूं तो स्क्रैपर के संदर्भों को हटाने के बाद? – ciferkey

संबंधित मुद्दे