【问题标题】:How to click Next button while scraping webpage抓取网页时如何单击下一步按钮
【发布时间】:2019-06-06 14:01:30
【问题描述】:

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


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

【问题讨论】:

    标签: python scrapy splash-screen scrapy-splash


    【解决方案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)
    

    【讨论】:

    • 好的,谢谢!坚持了一段时间,效果很好。一个小问题,没什么大不了的,但如果可能的话想解决它。不过,我会为此发布另一个问题,因为在此评论中发布太长了。
    猜你喜欢
    • 2019-10-09
    • 2017-05-10
    • 2021-04-09
    • 1970-01-01
    • 2020-12-27
    • 2015-01-05
    • 2016-11-16
    • 2020-06-30
    • 1970-01-01
    相关资源
    最近更新 更多