2016-05-12 8 views
-1

私はサイトからいくつかのページ区切りのページデータを抽出を通じて横断スパイダー構築しています:これは蜘蛛です http://www.usnews.com/education/best-global-universities/neuroscience-behaviorScrapyリュエルLinkExtractor次のページ

を:

# -*- coding: utf-8 -*- 
import scrapy 
from scrapy.contrib.spiders import Rule 
from scrapy.linkextractors import LinkExtractor 
from lxml import html 
from usnews.items import UsnewsItem 


class UniversitiesSpider(scrapy.Spider): 
    name = "universities" 
    allowed_domains = ["usnews.com"] 
    start_urls = (
     'http://www.usnews.com/education/best-global-universities/neuroscience-behavior/', 
     ) 

    #Rules = [ 
    #Rule(LinkExtractor(allow=(), restrict_xpaths=('.//a[@class="pager_link"]',)), callback="parse", follow= True) 
    #] 

    def parse(self, response): 
     for sel in response.xpath('.//div[@class="sep"]'): 
      item = UsnewsItem() 
      item['name'] = sel.xpath('.//h2[@class="h-taut"]/a/text()').extract() 
      item['location'] = sel.xpath('.//span[@class="t-dim t-small"]/text()').extract() 
      item['ranking'] = sel.xpath('.//div[3]/div[2]/text()').extract() 
      item['score'] = sel.xpath('.//div[@class="t-large t-strong t-constricted"]/text()').extract() 
      #print(sel.xpath('.//text()').extract() 
      yield item 

ページネーションを横断するためのルールコードが最初のサイトのデータを吐き出すだけなので、何もしていないようです。スパイダーが15ページすべてを通過し、サイトから4つのアイテム(名前、場所、ランキング、得点)を抽出するようにルールを正しく実装するにはどうすればよいですか?

+0

'rules'属性(あなたのコードのような" Rules "ではなく" rules "のように)を使うには、' scrapy.Spider.Spider'ではなく 'scrapy.CrawlSpider'をサブクラス化する必要があります。そして、@steveが彼の答えで言うように、 'CrawlSpider'の' parse'メソッドを再定義するべきではありません。 –

答えて

0

文書には、hereという警告に関連しているようです。具体的には:Ruleは、コールバックをparseと定義していますが、警告が表示されないようにするには、parseメソッドをオーバーライドすると(スパイダーで行ったように)、スパイダーは機能しなくなります。

これらは、使用するカスタムコールバックを定義する方法についてのドキュメントの例を示しています(基本的にはparseと名を付けないでください)。

Ruleのコメントを解除して投稿したコードでは実行されないため、実際に実行しているときにはRuleをコメント解除することを前提としています。

0

callback = 'parse' to callback = 'parse_start_url' CrawlSpiderクラスにはデフォルトで解析メソッドがあるため、再利用することは避けてください。

関連する問題