为了账号安全,请及时绑定邮箱和手机立即绑定

分页问题 - 无法理解日志

分页问题 - 无法理解日志

慕神8447489 2023-12-20 16:04:42
我在收到的日志中看不到任何错误,但只抓取了 108 个元素,尽管还有更多的项目需要抓取。所以,我猜这可能是分页的问题。但不知道如何解决。这是我缩短的蜘蛛:class AllbooksSpider(scrapy.Spider):    name = 'allbooks'    allowed_domains = ['www.digikala.com']        def start_requests(self):        yield scrapy.Request(url= 'https://www.digikala.com/search/category-book',            callback= self.parse)    def parse(self, response):        original_price=0                try:            for product in response.xpath("//ul[@class='c-listing__items js-plp-products-list']/li"):                title= product.xpath(".//div/div[2]/div/div/a/text()").get()                if product.xpath(".//div/div[2]/div[3]/div/div/del/text()"):                    original_price= int(str(product.xpath(".//div/div[2]/div[3]/div/div/del/text()").get().strip()).replace(',', ''))                    discounted_amount= original_price-discounted_price                else:                    original_price= print("not available")                    discounted_amount= print("not available")                 yield{                    'title':title,                    'discounted_amount': discounted_amount                    }             next_page= response.xpath('//*[@class="c-pager__item"]/../following-sibling::*//@href').extract_first()             if next_page:                yield scrapy.Request(response.urljoin(next_page))         except AttributeError:             logging.error("The element didn't exist")你能帮我了解问题是什么以及如何解决它吗?
查看完整描述

1 回答

?
HUX布斯

TA贡献1876条经验 获得超6个赞

问题是您的下一页链接选择器。

您当前的代码找到当前未激活的第一个分页链接,然后跟随其后的分页链接。


因此,这些是您的蜘蛛尝试跟踪的链接:


https://www.digikala.com/search/category-book/?sortby=4&pageno=3

https://www.digikala.com/search/category-book/?sortby=4&pageno=2

javascript:

解决此问题的一种方法是点击活动链接 ( )之后的链接@class="c-pager__item is-active"。


另一种产生更简单代码的方法是跟踪每个分页链接并让 dupefilter 完成其工作:


for link in response.css(".c-pager__item"):

    yield response.follow(link)


查看完整回答
反对 回复 2023-12-20
  • 1 回答
  • 0 关注
  • 49 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信