我在收到的日志中看不到任何錯誤,但只抓取了 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)
添加回答
舉報
0/150
提交
取消