抓取网页时如何单击“下一步”按钮

时间:2019-06-06 14:01:30

标签: python scrapy splash-screen scrapy-splash

尝试抓取以下网站http://www.starcitygames.com/catalog/category/Duel%20Decks%20Venser%20vs%20Koth,并且我需要能够单击“下一步”按钮才能继续下一页。我尝试了几种不同的方法,下面尝试了两种代码片段,但是没有奏效,我真的只是不知道该怎么做才能进入下一页。


 # 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)

1 个答案:

答案 0 :(得分:1)

无法使用CSS表达式按文本查找元素。因此,我强烈建议您在本部分中使用XPath:

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