繁体   English   中英

Scrapy蜘蛛只刮了2页,不去下一页

[英]Scrapy spider scrape only 2 pages and don't go to the next page

当我运行此代码时,蜘蛛只抓取 2 页并停止。 它不会转到下一页。

# -*- coding: utf-8 -*-
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()
            }

        next_page = response.xpath('//li[@class="arrow "]/a/@href').get()

        if next_page:
            yield scrapy.Request(url=response.urljoin(next_page), callback=self.parse)

将下一页选择器更改为此,看看它是否有效:

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

原因

从第二页开始,你有 2 li 类的arrow

  • 上一页
  • 下一页

您可以在此处阅读有关选择器的更多信息: https : //docs.scrapy.org/en/latest/topics/selectors.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM