2016-11-25 11 views
0

1つのWebサイトをクロールしますが、次のページをクロールすると機能しません。ここにスパイダーコードがありますか?どこが間違っていますか教えてください、ありがとうございます。次のページでの巡回クロールは機能しません

import scrapy 
from crawlAll.items import CrawlallItem 

class ToutiaoEssayJokeSpider(scrapy.Spider): 
    name = "duanzi" 
    allowed_domains = ["http://duanziwang.com"] 
    start_urls = ['http://duanziwang.com/category/duanzi/page/1'] 

    def parse(self, response): 
     for sel in response.xpath("//article[@class='excerpt excerpt-nothumbnail']"): 
      item = CrawlallItem() 
      item['Title'] = sel.xpath("//header/h2/a/text()").extract_first() 
      item['Text'] = sel.xpath("//p[@class='note']/text()").extract_first() 
      item['Views'] = sel.xpath("//p[1]/span[@class='muted'][2]/text()").extract_first() 
      item['Time'] = sel.xpath("//p[1]/span[@class='muted'][1]/text()").extract_first() 
      yield item 
     next_page = response.xpath("//ul/li[@class='next-page']/a/@href").extract_first() 
     if next_page is not None: 
      next_page = response.urljoin(next_page) 
      yield scrapy.Request(next_page, callback=self.parse) 

私は本当かどう場合にnext_page値をテストするために、印刷(にnext_page)を使用している、そしてそれは本当だ、それは私にこのようなリンクアドレス与える:http://duanziwang.com/category/duanzi/page/2」を、何が私のコードが悪いですの?

+0

あなたのクロールからコンソールログを共有できますか? (最後の統計情報付き) –

答えて

1

allowed_domainsのパラメータに問題があります。それは、この場合には、HTTPを含むべきではない、通常はそれだけで、トップレベルドメインでドメインを維持するために最善のIE domain.com

あなたがあなたのクモを実行し、ログを観察する場合は、この表示されます:

[scrapy] DEBUG: Filtered offsite request to 'duanziwang.com': <GET http://duanziwang.com/category/duanzi/page/2> 

をしたがって、試してみてください:

allowed_domains = ["duanziwang.com"] 
+0

ありがとう、私はこの問題を解決しました。 –

関連する問題