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

为何使用BeautifulSoup的select_one()报错,find()方法却正常运行?

解决BeautifulSoup中select_one替换find后的AttributeError问题

问题场景

原本使用嵌套find()方法能正常解析目标元素:

response = requests.get("https://near.org/blog/",headers=headers)
soup = BeautifulSoup(response.text, 'lxml').find("div", class_="bg-[#ffffff] grow rounded-br-[10px] rounded-bl-[10px] sm:rounded-bl-[0px] sm:rounded-tr-[10px] sm:rounded-br-[10px] py-[12px] px-[20px]").find("a").text.strip()
print(soup)

替换为select_one()后抛出错误:

response = requests.get("https://near.org/blog/",headers=headers)
soup = BeautifulSoup(response.text, 'lxml').select_one("div.bg-[#ffffff].grow.rounded-br-[10px].rounded-bl-[10px].sm:rounded-bl-[0px].sm:rounded-tr-[10px].sm:rounded-br-[10px].py-[12px].px-[20px] a").text.strip()
print(soup)

错误信息:

AttributeError: 'NoneType' object has no attribute 'text'

错误原因

select_one()遵循CSS选择器语法,而类名中的sm:rounded-bl-[0px]包含冒号——在CSS规则里,冒号是用于定义伪类/伪元素的特殊字符,直接写入会被解析为伪类规则,而非类名的一部分,导致选择器无法匹配到目标元素,返回None,调用text属性时自然触发报错。

而find()的class_参数是直接匹配完整的类属性字符串,不会解析CSS语法,因此能正确定位元素。

修复方案

有两种可行的解决方式:

方式1:转义类名中的冒号

在CSS选择器里,将冒号:转义为\:,让解析器识别为类名的一部分:

response = requests.get("https://near.org/blog/",headers=headers)
selector = "div.bg-[#ffffff].grow.rounded-br-[10px].rounded-bl-[10px].sm\:rounded-bl-[0px].sm\:rounded-tr-[10px].sm\:rounded-br-[10px].py-[12px].px-[20px] a"
soup = BeautifulSoup(response.text, 'lxml').select_one(selector).text.strip()
print(soup)

方式2:使用属性选择器匹配类名

通过[class*="类名片段"]的属性选择器,匹配包含目标类名片段的元素,无需写全所有类:

response = requests.get("https://near.org/blog/",headers=headers)
selector = 'div[class*="bg-[#ffffff]"][class*="grow"][class*="sm:rounded-bl-[0px]"] a'
soup = BeautifulSoup(response.text, 'lxml').select_one(selector).text.strip()
print(soup)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 18:52:35