亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

分頁問題 - 無法理解日志

分頁問題 - 無法理解日志

慕神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 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號