AttributeError:ResultSet对象没有属性"get_text".您可能正在将元素列表像单个元素一样对待 [英] AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of elements like a single element

查看:40
本文介绍了AttributeError:ResultSet对象没有属性"get_text".您可能正在将元素列表像单个元素一样对待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过代码段与Bs4进行解析,我得到了以下列表:

I got the following list of lists from parsing with Bs4 through the snippet:

details = [i.find_all('span', {'class':re.compile('item')}) for i in cars]

[[<span class="item">Red <small>col.</small></span>,
  <span class="item">120 <small>cc.</small></span>,
  <span class="item">Available <small>in four days</small></span>,
  <span class="item"><small class="txt-highlight-red">15 min</small></span>],
 [<span class="item">Blue <small>col.</small></span>,
  <span class="item">200 <small>cc.</small></span>,
  <span class="item">Available <small>in a week</small></span>,
  <span class="item">04 mar <small></small></span>],
 [<span class="item">Green <small>col.</small></span>,
  <span class="item">Available <small>immediately</small></span>,
  <span class="item"><small class="txt-highlight-red">2 hours</small></span>]]

要点是,并不是每个嵌套列表都具有完全相同的内容或相同的长度,所以我不想简化将文本放在唯一列表中的情况.

The point is that not every nested list has exactly the same content nor the same length, so I don't want to simplify getting the text in a sole list.

我尝试了以下代码:

bobo = []
for detail in details:
    for i in detail:
        bobo.append(i.text)

但是正如我所说的,它会产生以下输出:

But as I told it yields the following output:

[Red col., 120 cc., Available in four days, 15 min., Blue col., 200 cc., Available in a week, 04 mar , Green col., Available immediately, 2 hours]

预期产量为:

[[Red col., 120 cc., Available in four days, 15 min.],
[Blue col., 200 cc., Available in a week, 04 mar ],
[Green col., Available immediately, 2 hours]]

有帮助吗?

推荐答案

尝试一下

bobo = []
for detail in details:
    result = []
    for i in detail:
        result.append(i.text)
    bobo.append(result)

这篇关于AttributeError:ResultSet对象没有属性"get_text".您可能正在将元素列表像单个元素一样对待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆