2016-07-15 8 views
1

を私のクモを見つけることができません:Scrapyは、私は現在、以下の構造を有するscrapyプロジェクトを持っている現在のプロジェクトで

. 
├── articlescraper 
│   ├── __init__.py 
│   ├── __init__.pyc 
│   ├── items.py 
│   ├── items.pyc 
│   ├── pipelines.py 
│   ├── pipelines.pyc 
│   ├── scheduler.py 
│   ├── scheduler.pyc 
│   ├── settings.py 
│   ├── settings.pyc 
│   └── spiders 
│    ├── __init__.py 
│    ├── __init__.pyc 
│    ├── nujijspider.py 
│    └── nujijspider.pyc 
└── scrapy.cfg 

今私のscheduler.py内部で、私はこの関数を呼び出します。

from Queue import Queue 
import threading 
import time 
import sys 
import imp 
import scrapy 
from scrapy.crawler import CrawlerProcess 
from scrapy.utils.project import get_project_settings 


class Scheduler(object): 
    """Scheduler is the base class for the Scheduler 
     This class loops on the queue object and calls the needed crawlers from within 
     Reschedules articles to be crawled again 
    """ 
    def __init__(self): 
     self.articleInformation = {} 
     self.taskQueue = Queue() 

    def append_work(self, work): 
     if work['Url'] not in self.articleInformation: 
      self.articleInformation[work['Id']] = work 

     print self.articleInformation 

    def schedule(self): 
     article = self.taskQueue.get() 

settings = get_project_settings() 
process = CrawlerProcess(settings) 
process.crawl("articlecommentspider",url="///") 
process.start() 

しかし、これはscrapyからこのエラーが発生:

File "/usr/local/lib/python2.7/site-packages/scrapy/spiderloader.py", line 43, in load 
    raise KeyError("Spider not found: {}".format(spider_name)) 
KeyError: 'Spider not found: articlecommentspider' 

スパイダー:

class ArticleCommentSpider(scrapy.Spider): 
    """ArticleCommentSpider Can look for all the the comments on an article page 
     Those article pages are specific to www.nujij.nl and nu.nl related websites 
    """ 
    name = 'articlecommentspider' 
    allowed_domains = ['nujij.nl'] 

def __init__(self, *args, **kwargs): 
    super(ArticleCommentSpider, self).__init__(*args, **kwargs) 
    arg = args.get('url') 
    if not arg: 
     print arg 
    self.start_urls = arg 


def parse(self,response): 
    title = response.xpath("//h1"+matchClass('title')+"//text()").extract()[1] ## Title is weird defined inside Nujij.nl (<h1 class="title">) 
    articleId = prog.search(response.url).group().split('.')[0] ## This regex matches things like (873238.lynkx in url) 

    response.replace(body=response.body.replace('<br>', '\n')) # Needed for comments which have alot of <br> tags 
    for item in response.xpath('//ol[@class="reacties"]//li'+ matchClass('hidenum')): ## Every list item underneath the reactions 
     commentId = item.xpath('@id').extract_first() ## Id from the first list item (unique on every article) 
     c = item.xpath('.//div[@class="reactie-body "]/text()').extract() 
     c = ''.join(map(unicode.strip, c)) 
     date = item.xpath('normalize-space(.//span[@class="tijdsverschil"])').extract() 
     date = dateparser.parse("".join(date)) 

     articleComment = Comment() 
     articleComment['Id'] = articleId+"+"+str(commentId) 
     articleComment['Source'] = str(title) 
     articleComment['IndexedAt'] = date 
     articleComment['Url'] = response.url 
     articleComment['Parser'] = "nujij.nl" 
     articleComment['Content'] = str(c) 
     articleComment['Subject'] = { 
      "url" : response.url, 
      "title": str(title) 
     } 
     print articleComment 

スクレイパーリストを使用してスクレイパーをリストすると、その両方が得られます。スケジューラファイルは、articlescraperプロジェクト内にもあります。このプロセス内からスクレーパーを呼び出せない場合はどうすればいいですか

答えて

0

クモの名前ではなく、scrapy.spiderクラスを指定する必要があります。

from articlescraper.nujijspider import NujijSpider 
process.crawl(NujijSpider) 

official documentation here

+0

をチェックしてください。また、これは プロセス= CrawlerProcess(get_project_settings()) はscrapy.utils.projectインポートget_project_settingsからscrapy.crawlerインポートCrawlerProcess から 'ドキュメント であっ立ちます# 'followall'は、プロジェクトのスパイダーの名前です。 process.crawl( 'followall'、domain = 'scrapinghub.com') process.start()#クロールが完了するまでスクリプトがブロックされます。 ' –

+0

hmm、しかし、私は 'testspiders'リポジトリでそれを試してみました。実際にはうまく動作します。構造はあなたのところではほとんど同じです。 'scrapy list'は正しいスパイダー名を返しますか? – Granitosaurus

+0

はい、私はそれが奇妙なtoughtだった理由だった。私はあなたのアプローチに変更して、今すぐ動作します! –

関連する問題