【问题标题】:Extract all next pages from website with Scrapy使用 Scrapy 从网站中提取所有下一页
【发布时间】:2020-09-02 08:02:50
【问题描述】:

我正在尝试从以下 URL 抓取所有页面:https://www.residentialpeople.com/za/property-for-sale/cape-town/?country=za&listing_type=residential&transaction_type=sale&longitude=18.49144&latitude=-33.98983&size_qualifier=square_feet&location_slug=cape-town&sort_by=closest_to_farthest&offset=0&limit=10&active=1&_radius_expansion=0&_location=Cape%20Town,%20South%20Africa&status_available_only=0

但是,它只能抓取前 4 页,然后就停止了 代码如下:

    def parse(self, response):
        # follow links to property pages
        for href in response.xpath('//div[@class="listings-item-bottom"]//a[@class="link link--minimal"]/@href').getall():
            yield response.follow(href, self.parse_property)

        # follow pagination links
        old_offset = self.page_counter
        old_offset = str(old_offset) + '0' if old_offset != 0 else str(old_offset)

        try:
            max_page = int(''.join(response.css('div.custom-pagination-select::text').re(r'\d+')))
        except:
            max_page = None

        self.page_counter += 1
        if self.page_counter < max_page:
            new_offset = str(self.page_counter) + '0'

            next_page_url = response._get_url().replace(f'offset={old_offset}', f'offset={new_offset}')
            next_page = response.urljoin(next_page_url)
            yield scrapy.Request(next_page, callback=self.parse)

有人对这里可能出现的问题有任何建议吗? 提前谢谢!

【问题讨论】:

    标签: python xpath web-scraping scrapy web-crawler


    【解决方案1】:

    我认为您唯一需要替换的是 URL 中的偏移量以到达下一页。

    显然,您可能希望对此进行概括,以便您始终可以根据每次搜索的结果数量来获取每个页面。

    代码示例

    def parse(self,response):
        for href in response.xpath('//div[@class="listings-item-bottom"]//a[@class="link link--minimal"]/@href').getall():
            yield response.follow(href, self.parse_property)
        
        results_num = int(response.xpath('//div[@class="total-available-results"]/span/text()').get())
    
        for i in range(10,results_num+10,10):
            url = f'https://www.residentialpeople.com/za/property-for-sale/cape-town/?country=za&listing_type=residential&transaction_type=sale&longitude=18.49144&latitude=-33.98983&size_qualifier=square_feet&location_slug=cape-town&sort_by=closest_to_farthest&offset={i}&limit=10&active=1&status_available_only=0&_radius_expansion=0&_location=Cape%20Town,%20South%20Africa'
            yield scrapy.Request(url=url, callback=self.parse,dont_filter=True)
    

    解释

    results_num 为我们提供了属性总数。我们使用从偏移量 10 开始的 for 循环,在这种情况下最多为 15720,请记住我们需要将 +10 添加到结束参数,因为范围内的结束参数不包括最多 results_num。 10 的 step 参数就是我们想要的。

    我们为每个请求动态创建 URL,在 for 循环中使用 f 字符串,我们为变量 i 赋予上面我们想要解释的值。我们可以使用它为每次 for 循环迭代所需的偏移量创建新的字符串 URL。然后,我们可以通过回调解析函数在每次迭代中发出请求。请记住,因为 URL 的基础是相同的,scrapy 会过滤这个,所以在 Request 中我们指定 dont_filter=True

    【讨论】:

    • 您好,非常感谢您的解释!当我尝试这个时,我仍然只刮掉了几个属性,而不是 1572 页的所有属性。还有其他问题吗? @AaronS
    • 忘记添加 yield 声明了,抱歉!代码已更新。
    • 不客气!如果您以前没有使用过 f 字符串,我强烈推荐它们!对于更改字符串中的多个变量没有那么有用,但在 python 3.6 中引入,因为格式有点麻烦。 realpython.com/python-f-strings 很有用!基本上让这段代码变得简单了很多。
    • 是啊,挺好看的,没见过!再次感谢您的精彩解释! @AaronS
    • 很抱歉打扰您,但也许您可以帮我做最后一件事:我什至无法从该网站的主页上获得结果:myproperty.co.za/… 抱歉打扰你,感谢你的时间:) @AaronS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    相关资源
    最近更新 更多