2017-07-08 10 views
0

Hすべてに行きたくありません。それは、次のページに行きたいdoesntの:Scrapyスパイダーは、ここではそれについての私の前の質問があり、私はscrapyクローラを書いています</p> <p>、次のページ

は今、私は別の問題が生じています

from scrapy.contrib.spiders import CrawlSpider 
from scrapy.selector import Selector 
from scrapy.http import Request 

class YourCrawler(CrawlSpider): 
    name = "bookstore_2" 
    start_urls = [ 
    'https://example.com/materias/?novedades=LC&p', 
    ] 
    allowed_domains = ["https://example.com"] 

    def parse(self, response): 
     # go to the urls in the list 
     s = Selector(response) 
     page_list_urls = s.xpath('///*[@id="results"]/ul/li/div[1]/h4/a[2]/@href').extract() 
     for url in page_list_urls: 
      yield Request(response.urljoin(url), callback=self.parse_following_urls, dont_filter=True) 

    # For the urls in the list, go inside, and in div#main, take the div.ficha > div.caracteristicas > ul > li 
    def parse_following_urls(self, response): 
     #Parsing rules go here 
     for each_book in response.css('div#main'): 
      yield { 
      'book_isbn': each_book.css('div.ficha > div.caracteristicas > ul > li').extract(), 
      } 

     # Return back and go to bext page in div#paginat ul li.next a::attr(href) and begin again 
     next_page = response.css('div#paginat ul 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) 

そして、それが動作すると、最初のページのリンクのデータを保存しますが、行くしようとすると、それが失敗しましたエラーなしで次のページに移動します。これは、ログです:

… 
2017-07-08 17:17:25 [scrapy.core.scraper] DEBUG: Scraped from <200 https://example.com/book/?id=9780143039617> 
{'book_isbn': [u'<li>Editorial: <a href="/search/avanzada/?go=1&amp;editorial=Penguin%20Books">Penguin Books</a></li>', u'<li>P\xe1ginas: 363</li>', u'<li>A\xf1o: 2206</li>', u'<li>Precio: 14.50 \u20ac</li>', u'<li>EAN: 9780143039617</li>']} 
2017-07-08 17:17:25 [scrapy.core.engine] INFO: Closing spider (finished) 
2017-07-08 17:17:25 [scrapy.extensions.feedexport] INFO: Stored json feed (10 items) in: bookstore_2.json 
2017-07-08 17:17:25 [scrapy.statscollectors] INFO: Dumping Scrapy stats: 

は私が私の最初のクモで、この次のページセクションを使用し、それが働いていました。なぜこのことが起こるのか?

答えて

1

parse_following_urlsメソッドの代わりにparseメソッドの最後にページングロジックを配置する必要があります。ページングのリンクはメインページであり、ブックの詳細ページではありません。また、私はallowed_domainsからスキームを削除しなければならなかった。最後に、scrapyモジュールがインポートされていないので、parseメソッドの最後にRequestという結果が出ることに注意してください。スパイダーは次のようになります。

from scrapy.contrib.spiders import CrawlSpider 
from scrapy.selector import Selector 
from scrapy.http import Request 

class YourCrawler(CrawlSpider): 
    name = "bookstore_2" 
    start_urls = [ 
    'https://lacentral.com/materias/?novedades=LC&p', 
    ] 
    allowed_domains = ["lacentral.com"] 

    def parse(self, response): 
     # go to the urls in the list 
     s = Selector(response) 
     page_list_urls = s.xpath('///[@id="results"]/ul/li/div[1]/h4/a[2]/@href').extract() 
     for url in page_list_urls: 
      yield Request(response.urljoin(url), callback=self.parse_following_urls, dont_filter=True) 

     # Return back and go to bext page in div#paginat ul li.next a::attr(href) and begin again 
     next_page = response.css('div#paginat ul li.next a::attr(href)').extract_first() 
     if next_page is not None: 
      next_page = response.urljoin(next_page) 
      yield Request(next_page, callback=self.parse) 

    # For the urls in the list, go inside, and in div#main, take the div.ficha > div.caracteristicas > ul > li 
    def parse_following_urls(self, response): 
     #Parsing rules go here 
     for each_book in response.css('div#main'): 
      yield { 
       'book_isbn': each_book.css('div.ficha > div.caracteristicas > ul > li').extract(), 
      } 
+0

ありがとう!あなたのコードで試してみたら、ValueErrorを返します:XPathエラー:/// [@ id = "results"]/ul/li/div [1]/h4/a [2]/@ href'の式が無効です。私は '*'が必要だと思う。しかし、一旦 '*'を追加すると、それは動作し始めました!私のコードを並べ替えるとうまくいかないので、私はもう一度チェックします。 –

関連する問題