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

Beautiful Soup find返回None求助:匹配price-normal失败

问题原因与解决方案

核心原因

你的问题源于BeautifulSoup的class精确匹配规则:

  • 当你用row.find('div', {'class': 'price-normal selenium-price-normal'})时,是精确匹配元素class属性的完整字符串值,恰好对应目标元素的类组合,因此能成功定位。
  • 而row.find('div', {'class': 'price-normal'})同样是精确匹配class属性的完整值,但目标元素的class实际是price-normal selenium-price-normal(两个类名的组合),并非单独的price-normal,所以匹配失败返回None,调用text时自然触发AttributeError。

正确的匹配写法

要匹配包含price-normal类的元素,改用以下两种方式:

  1. 使用BeautifulSoup专属的class_参数(支持匹配单个类):
row.find('div', class_='price-normal')
  1. 使用CSS选择器(更贴合前端选择逻辑,推荐):
row.select_one('div.price-normal')

额外说明

WebDriverWait无法解决这个问题,因为等待只是确保元素完成加载,但你的匹配逻辑本身不符合BeautifulSoup的规则。另外,页面中可能存在仅含price-normal的隐藏占位元素,但结合你提供的HTML结构,核心问题还是class匹配的规则差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 13:34:38