Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python BeautifulSoup: 'list_iterator' object is not subscriptable

I'm trying to extract the text inside from the following html structure:

<div class="account-age">
    <label></label>
    <div>
        <div>
             <span>Text to extract</span>
        </div>
    </div>
</div>

I have the following Beautiful Soup code to do it:

from bs4 import BeautifulSoup as bs

soup = bs(html, "lxml")
div = soup.find("div", {"class": "account-age"})
span = div.children[1].children[0].children[0]
text = span.get_text()

Unfortunately, Beautiful Soup is throwing the error: 'list_iterator' object is not subscriptable. How can I fix this to extract the text I need?

like image 657
Brinley Avatar asked Aug 04 '26 13:08

Brinley


1 Answers

You might do this by directly chaining the tags from the root div:

div.div.div.span.get_text()
# u'Text to extract'
like image 86
Psidom Avatar answered Aug 07 '26 03:08

Psidom