【问题标题】:scrapy pagination not following the link to next pagescrapy分页没有跟随到下一页的链接
【发布时间】:2021-04-08 18:18:02
【问题描述】:

所以我在网上学习了一个教程,但内容已经过时了。我不知道为什么它不会刮掉下一页?这是代码。

我还设置了 ROBOTSTXT_OBEY = False

import scrapy


class EcigPageSpider(scrapy.Spider):
    name = 'ecig_page'
    allowed_domains = ['www.cigabuy.com/']
    start_urls = [
        'https://www.cigabuy.com/consumer-electronics-c-56_75-pg-1.html'
    ]

    def parse(self, response):
        for product in response.xpath("//div[@class='p_box_wrapper']"):
            title = product.xpath(".//a[@class='p_box_title']/text()").get()
            url = product.xpath(".//a[@class='p_box_title']/@href").get()
            discounted_price = product.xpath(".//div[@class='p_box_price cf']/span[1]/text()").get()
            original_price = product.xpath(".//div[@class='p_box_price cf']/span[2]/text()").get()

            yield {
                'title': title,
                'url': url,
                'discounted_price': discounted_price,
                'original_price': original_price,
            }

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

        if next_page:
            yield response.follow(url=next_page, callback=self.parse)

我也尝试将 if next page: body 更改为

yield scrapy.Request(url=next_page, callback=self.parse)

还是不行。

【问题讨论】:

    标签: python pagination scrapy


    【解决方案1】:

    我现在明白了,只是我需要删除这一行中多余的“/”

    allowed_domains = ['www.cigabuy.com/']

    确保不要包含斜线

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      相关资源
      最近更新 更多