Rodrigo Vargas
Rodrigo Vargas

Reputation: 283

AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of elements like a single element

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.

I've tried this code:

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]

While the expected ouput is:

[[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]]

Any help?

Upvotes: 0

Views: 544

Answers (1)

Manali Kagathara
Manali Kagathara

Reputation: 761

try this

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

Upvotes: 1

Related Questions