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

BeautifulSoup二次调用find_all筛选数据时报ResultSet无对应属性错误

错误原因

  • find_all() 方法返回的是ResultSet类列表对象,包含所有匹配到的父级元素,列表本身不支持调用find_all()查找方法,只有单个DOM元素节点才支持该操作。

解决方案

根据实际页面结构选对应处理方式即可:

方式1:父级元素唯一的场景

把第一次的find_all替换为find,直接获取单个父级元素后再做二次筛选,同时建议加请求头避免反爬拦截,修正后代码如下:

import requests
from bs4 import BeautifulSoup

def main():
    get_stock_data()

def get_stock_data():
    # 模拟浏览器请求头,避免被站点反爬拦截
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    r = requests.get('https://robinhood.com/stocks/WISH', headers=headers)
    soup = BeautifulSoup(r.content, 'html.parser')
    # 用find取单个父级元素
    parent_node = soup.find(class_="css-ktio0g")
    # 加判空逻辑,避免父级元素不存在时报错
    if parent_node:
        target = parent_node.find_all(class_="css-6e9xj2")
        print(target)
    else:
        print("未匹配到对应父级元素")

main()

方式2:父级元素有多个的场景

遍历第一次筛选拿到的父级元素列表,逐个查找目标子元素后汇总结果:

# 仅展示核心筛选逻辑,请求部分同上
parent_nodes = soup.find_all(class_="css-ktio0g")
target_list = []
for parent in parent_nodes:
    target_list.extend(parent.find_all(class_="css-6e9xj2"))
print(target_list)

内容的提问来源于stack exchange,提问作者fletcher.l.collins

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 05:54:04