2017-01-18 6 views
2

したがって、私はスパイダーの例と非常によく似ています。スクリーニング・ページング・タイミング・エラー

次のページに行く前にクモがすべての引用をクロールするようにします。また、1秒間に1つの見積もりしか解析しないようにしたい。したがって、ページに20の引用符がある場合は、引用符を掻き集めるのに20秒かかり、次のページに行くのに1秒かかります。

現時点では、私の現在の実装は、実際に見積もり情報を取得する前に各ページを最初に反復しています。ここで

import scrapy 

class AuthorSpider(scrapy.Spider): 
name = 'author' 

start_urls = ['http://quotes.toscrape.com/'] 

def parse(self, response): 
    # follow links to author pages 
    for href in response.css('.author+a::attr(href)').extract(): 
     yield scrapy.Request(response.urljoin(href), 
          callback=self.parse_author) 

    # follow pagination links 
    next_page = response.css('li.next a::attr(href)').extract_first() 
    if next_page is not None: 
     next_page = response.urljoin(next_page) 
     yield scrapy.Request(next_page, callback=self.parse) 

def parse_author(self, response): 
    def extract_with_css(query): 
     return response.css(query).extract_first().strip() 

    yield { 
     'name': extract_with_css('h3.author-title::text'), 
     'birthdate': extract_with_css('.author-born-date::text'), 
     'bio': extract_with_css('.author-description::text'), 
    } 

あなたはscrapy.Requestsをもたらしているかオーケストレーションができ、私のsettings.pyファイル

ROBOTSTXT_OBEY = True 
CONCURRENT_REQUESTS = 1 
DOWNLOAD_DELAY = 2 

答えて

1

の基本です。

たとえば、次のページリクエストを作成することができますが、すべての作成者リクエストがアイテムのスクレイピングを終了したときにのみリクエストします。

例:

import scrapy 

# Store common info about pending request 
pending_authors = {} 

class AuthorSpider(scrapy.Spider): 
name = 'author' 

start_urls = ['http://quotes.toscrape.com/'] 

def parse(self, response): 

    # process pagination links 
    next_page = response.css('li.next a::attr(href)').extract_first() 
    next_page_request = None 
    if next_page is not None: 
     next_page = response.urljoin(next_page) 
     # Create the Request object, but does not yield it now 
     next_page_request = scrapy.Request(next_page, callback=self.parse) 

    # Requests scrapping of authors, and pass reference to the Request for next page 
    for href in response.css('.author+a::attr(href)').extract(): 
     pending_authors[href] = False # Marks this author as 'not processed' 
     yield scrapy.Request(response.urljoin(href), callback=self.parse_author, 
          meta={'next_page_request': next_page_request}) 


def parse_author(self, response): 
    def extract_with_css(query): 
     return response.css(query).extract_first().strip() 

    item = { 
     'name': extract_with_css('h3.author-title::text'), 
     'birthdate': extract_with_css('.author-born-date::text'), 
     'bio': extract_with_css('.author-description::text'), 
    } 

    # marks this author as 'processed' 
    pending_authors[response.url] = True 

    # checks if finished processing of all authors 
    if len([value for key, value in pending_authors.iteritems() if value == False]) == 0: 
     yield item 
     next_page_request = response.meta['next_page_request'] 

     # Requests next page, after finishinr all authors 
     yield next_page_request 
    else: 
     yield item 
+0

が動作していないようでした。グローバル名 'pending_authors'は定義されていません。 – Nytrox

+0

JKすべてのインスタンスに対してself.pending_authorsである必要があります。 – Nytrox

+0

質問に続きます:このメソッドでは、すでに解析されたページが見つかると次のリンクに移動しようとしません。これを無効にする方法を知っていますか(前に見た作家を掻き集めないで、ページを繰り返しています) – Nytrox

関連する問題