Skip to content Skip to sidebar Skip to footer

Python BeautifulSoup FindAll By "class" Attribute

I want to do the following code, which is what BS documentation says to do, the only problem is that the word 'class' isn't just a word. It can be found inside HTML, but it's also

Solution 1:

Your problem seems to be that you expect find_all in the soup to find an exact match for your string. In fact:

When you search for a tag that matches a certain CSS class, you’re matching against any of its CSS classes:

You can properly search for a class tag as @alKid said. You can also search with the class_ keyword arg.

soup.find_all('ul', class_="score")

Solution 2:

Here is how to do it:

soup.find_all('ul', {'class':"score"})

Solution 3:

If OP is interested in getting the finalScore by going through ul you could solve this with a couple of lines of gazpacho:

from gazpacho import Soup

html = """\
<div>
    <ul class="score header" id="400488971-linescoreHeader" style="display: block">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li id="400488971-lshot"> </li>
      <li class="finalScore">T</li>
    </ul>
<div>
"""

soup = Soup(html)
soup.find("ul", {"class": "score"}).find("li", {"class": "finalScore"}).text

Which would output:

'T'

Post a Comment for "Python BeautifulSoup FindAll By "class" Attribute"