Tim
Tim

Reputation: 201

How to click Next button while scraping webpage

Trying to scrape following website, http://www.starcitygames.com/catalog/category/Duel%20Decks%20Venser%20vs%20Koth, and I need to be able to click the next button to continue to the following page. I tried several different things, I have two of code snippets I tried below, but none have worked and I really just do not know what to do to get to the next page.


 # Scraping
    def parse(self, response):
        item = GameItem()
        saved_name = ""

        item["Category"] = response.css("span.titletext::text").extract()
        for game in response.css("tr[class^=deckdbbody]"):
            saved_name  = game.css("a.card_popup::text").extract_first() or saved_name
            item["card_name"] = saved_name.strip()

            if item["card_name"] != None:
                saved_name = item["card_name"].strip()
            else:
                item["card_name"] = saved_name

            item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
            item["stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
            item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()

            yield item
            next_page = response.css('#content > div:last-of-type > a\(\@href\):last-of-type').get()
            if next_page is not None:
                yield response.follow(next_page, self.parse)

 # Scraping
    def parse(self, response):
        item = GameItem()
        saved_name = ""

        item["Category"] = response.css("span.titletext::text").extract()
        for game in response.css("tr[class^=deckdbbody]"):
            saved_name  = game.css("a.card_popup::text").extract_first() or saved_name
            item["card_name"] = saved_name.strip()

            if item["card_name"] != None:
                saved_name = item["card_name"].strip()
            else:
                item["card_name"] = saved_name

            item["Condition"] = game.css("td[class^=deckdbbody].search_results_7 a::text").get()
            item["stock"] = game.css("td[class^=deckdbbody].search_results_8::text").extract_first()
            item["Price"] = game.css("td[class^=deckdbbody].search_results_9::text").extract_first()

            yield item

        next_page = response.css('table+ div a:nth-child(8)::attr(href)').get()
        if next_page is not None:
            yield response.follow(next_page, self.parse)

Upvotes: 0

Views: 109

Answers (1)

gangabass
gangabass

Reputation: 10666

There is no way to find element by text using CSS expression. That's why I highly recommend you to use XPath for this part:

next_page = response.xpath('//a[contains(., "- Next>>")]/@href').get()
if next_page is not None:
    yield response.follow(next_page, self.parse)

Upvotes: 1

Related Questions