如何添加下一页循环抓取功能

问题描述 投票:0回答:1

这是我的代码。现在,它只能从一页中抓取信息。我将如何添加页面循环?

  import scrapy


class P1Spider(scrapy.Spider):
    name = 'p1'
    allowed_domains = ['www.visit.ferienmesse.ch']
    start_urls = ['https://www.visit.ferienmesse.ch/de/aussteller']

    def parse(self, response):

        for data in response.xpath('//ul[@class="ngn-search-list ngn-mobile-filter"]/li'):
            yield {
                'Link': response.urljoin(data.xpath('.//h2[@class="ngn-content-box-title"]/a/@href').get()),
                'Title': data.xpath('//h2[@class="ngn-content-box-title"]/a/bdi/text()').get(),
                'Address': data.xpath('.//span[@class="ngn-hallname"]/text()').get(),
                'Code': data.xpath('.//span[@class="ngn-stand"]/text()').get()
            }
python scrapy
1个回答
1
投票

只需在下面添加此代码,希望它能工作。

 next_page = response.css('.pagination li.arrow a[rel="next"]::attr(href)').get()

    if next_page:
        yield scrapy.Request(url=response.urljoin(next_page), callback=self.parse)
© www.soinside.com 2019 - 2024. All rights reserved.