Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading