使用Selenium爬取IMDB评论时无法定位评分元素的求助
IMDB移动端评论评分元素定位失败解决方法
问题说明
爬取IMDB移动端评论页面时,能正常获取评论标题、内容、日期和用户名,但定位评分元素时一直触发selenium.common.exceptions.NoSuchElementException异常。
评分元素HTML结构
<div> <div class="inline-rating"> <div class="ipl-ratings-bar"> <span class="rating-other-user-rating"> <svg class="ipl-icon ipl-star-icon " xmlns="http://www.w3.org/2000/svg" fill="#000000" height="24" viewBox="0 0 24 24" width="24"> <path d="M0 0h24v24H0z" fill="none"></path> <path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"></path> <path d="M0 0h24v24H0z" fill="none"></path> </svg> <span>4</span> <span class="point-scale">/10</span> </span> </div> </div> <a href="/review/rw3316476/?ref_=tt_urv" class="title"> The Most Over-Rated Show I Have Ever Seen</a> </div>
已尝试的定位代码
service = Service(executable_path="chromedriver.exe") driver = webdriver.Chrome(service=service) url = 'https://m.imdb.com/title/tt0903747/reviews?ref_=tt_urv' driver.get(url) review_elements = driver.find_elements(By.CLASS_NAME, 'review-container') # 以下定位方式均触发异常 rating = review_element.find_element(By.CLASS_NAME, 'inline-rating') rating = review_element.find_element(By.CLASS_NAME, 'ipl-ratings-bar') rating = review_element.find_element(By.CSS_SELECTOR, '.rating-other-user-rating span') rating = review_element.find_element(By.CLASS_NAME,'rating-other-user-rating')
解决办法
1. 处理无评分的评论
部分用户可能未给出评分,直接用find_element会报错,改用find_elements判断是否存在该元素:
for review_element in review_elements: # 定位评分数字所在的span rating_items = review_element.find_elements(By.CSS_SELECTOR, '.rating-other-user-rating > span:nth-child(2)') # 存在则取文本,否则标记无评分 rating = rating_items[0].text if rating_items else "无评分" print(rating)
2. 添加显式等待确保元素加载
页面评论可能是动态渲染的,添加等待时间让元素完全加载:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver.get(url) # 等待所有评论容器加载完成 WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located((By.CLASS_NAME, 'review-container')) ) review_elements = driver.find_elements(By.CLASS_NAME, 'review-container') for review_element in review_elements: try: # 等待当前评论的评分元素出现(最多等5秒) rating = WebDriverWait(review_element, 5).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.rating-other-user-rating > span:nth-child(2)')) ).text except: rating = "无评分" print(rating)
3. 优化定位表达式
- 避免用
CLASS_NAME定位包含多个类名的元素,改用CSS选择器更精准 - 直接定位评分数字的
<span>,用.rating-other-user-rating > span:nth-child(2)可以精准匹配到评分值(比如示例中的4)
内容的提问来源于stack exchange,提问作者Paula Gryglewska
相关产品推荐
相关产品推荐

