Get BeautifulSoup Class attribute value

I want to get the value of only 3.5,4.3 and 2.5 from the below structural html content using BeautifulSoup. Please help how to scrap the value.

<div class="abc">3.5<img class="def" src=""</div>
<div class="abc">4.3<img class="def" src=""</div>
<div class="abc">2.5<img class="def" src=""</div>

Here’s a quick snippet on how to achieve this. Since you didn’t provide any code, I don’t know where your html is coming from, so I’ll just set your given html to a variable.

from bs4 import BeautifulSoup

html_content = """
<div class="abc">3.5<img class="def" src=""</div>
<div class="abc">4.3<img class="def" src=""</div>
<div class="abc">2.5<img class="def" src=""</div>
"""

# Initialize html parser
soup = BeautifulSoup(html_content, 'html.parser')

# Find all div elements with class 'abc'
div_elements = soup.find_all('div', class_='abc')

# Extract the text from each div element and print the value
for div in div_elements:
    value = div.text.strip()  # Get the text and remove leading/trailing spaces
    print(value)

This will print out:

3.5
4.3
2.5

Leave a Comment