1

I am scraping a webpage using scrapy that has multiple pages of information and I need the program to click the next button and then scrape the next page and then keep doing that until all the pages have been scraped. But I cannot figure out how to do that, I can only scrape the first page.

from scrapy_splash import SplashRequest
from ..items import GameItem

class MySpider(Spider):
        name = 'splash_spider' # Name of Spider
        start_urls = ['http://www.starcitygames.com/catalog/category/10th%20Edition'] # url(s)
        def start_requests(self):
                for url in self.start_urls:
                        yield SplashRequest(url=url, callback=self.parse, args={"wait": 3})
        #Scraping
        def parse(self, response):
                item = GameItem()
                for game in response.css("tr"):
                        # Card Name
                        item["Name"] = game.css("a.card_popup::text").extract_first()
                        # Price
                        item["Price"] = game.css("td.deckdbbody.search_results_9::text").extract_first()
                        yield item
Tim
  • 191
  • 2
  • 28

3 Answers3

1

Documentation is pretty explicit about it :

from scrapy_splash import SplashRequest
from ..items import GameItem

class MySpider(Spider):
        name = 'splash_spider' # Name of Spider
        start_urls = ['http://www.starcitygames.com/catalog/category/10th%20Edition'] # url(s)
        def start_requests(self):
            for url in self.start_urls:
                yield SplashRequest(url=url, callback=self.parse, args={"wait": 3})
        #Scraping
        def parse(self, response):
            item = GameItem()
            for game in response.css("tr"):
                # Card Name
                item["Name"] = game.css("a.card_popup::text").extract_first()
                # Price
                item["Price"] = game.css("td.deckdbbody.search_results_9::text").extract_first()
                yield item

            next_page = response.css(<your css selector to find next page>).get()
            if next_page is not None:
                yield response.follow(next_page, self.parse)
Corentin Limier
  • 4,946
  • 1
  • 13
  • 24
1

You could use css selector to target the next button

a:nth-of-type(7)

with nth-child

a:nth-child(8)
QHarr
  • 83,427
  • 12
  • 54
  • 101
1

You can get it working like below:

import scrapy

class GameSpider(scrapy.Spider):
        name = 'game_spider'
        start_urls = ['http://www.starcitygames.com/catalog/category/10th%20Edition'] 

        def parse(self, response):
                for game in response.css("tr.deckdbbody_row"):
                        yield {
                                'name': game.css("a.card_popup::text").get(),
                                'price': game.css(".search_results_9::text").get(),
                        }
                next_page_url = response.css('table+ div a:nth-child(8)::attr(href)').get()
                if next_page_url is not None:
                        yield response.follow(next_page_url, self.parse)
Mehrdad
  • 131
  • 5