I have div with just class name

25 views
Skip to first unread message

Ahmer Zunair

unread,
Jul 15, 2022, 11:43:28 AMJul 15
to beautifulsoup
datalist = soup.findAll(lambda tag: tag.name=="div" and tag.get("class") == "card-content text-small description mini-scroll")[0].string
       
Error:
list index out of range 

facelessuser

unread,
Jul 18, 2022, 9:27:29 AMJul 18
to beautifulsoup

I think your problem is that for HTML, tag.get("class") returns a list of classes, not a string. Something like this would work:

datalist = soup.find_all(lambda tag: tag.name=="div" and tag["class"] == ['card-content', 'text-small', 'description', 'mini-scroll'])[0]

With that said, I personally find using selectors, in this case, more straight forward:

datalist = soup.select('div.card-content.text-small.description.mini-scroll')[0]
Reply all
Reply to author
Forward
0 new messages