You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scrapy无法获取页面星级评分的技术求助

Fixing Your Scrapy Star Rating Selection Issue

Let me walk you through what's going wrong here and how to fix it! Your current selectors aren't working for two key reasons: you're targeting a non-existent parent element, and your XPath uses an invalid function.

Correct CSS Selectors (Simplest Approach)

Looking at your HTML, the selected stars live under the .search-item-info container, not #info-panel (which doesn't appear anywhere in your markup). Use these CSS selectors to count the selected stars:

# Directly target all .star.selected elements under the main container
len(response.css('.search-item-info .star.selected'))

# Even more precise (targets only stars inside the star-rating block)
len(response.css('.search-item-info .star-rating .stars .star.selected'))

Correct XPath Alternative

If you prefer using XPath, forget the has-class function—it's not standard XPath. Instead, check for the presence of both classes properly:

# Basic version (works if you don't have overlapping class names)
len(response.xpath('//div[@class="search-item-info"]//span[contains(@class, "star") and contains(@class, "selected")]'))

# Robust version (avoids partial class matches, e.g., "starred" vs "star")
len(response.xpath('//div[@class="search-item-info"]//span[contains(concat(" ", normalize-space(@class), " "), " star ") and contains(concat(" ", normalize-space(@class), " "), " selected ")]'))

Why Your Original Selectors Failed

  • #info-panel doesn't exist: Your HTML uses div.search-item-info as the parent container, not an element with id="info-panel". That's why both your selectors couldn't find anything.
  • has-class is invalid: Scrapy doesn't include this custom XPath function by default. Stick to standard XPath syntax or use CSS selectors (which are cleaner for class-based queries).

Bonus: Grab the Exact Rating Score

If you need the actual rating value (3.92) instead of just the count of filled stars, you can pull it directly from the title attribute of the .star-rating span:

response.css('.star-rating::attr(title)').get()

内容的提问来源于stack exchange,提问作者user13368804

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:37:33