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

extract an element from the class with Beautiful Soup

how would you extract the "2021-12-30" from data-dat?

<td class="select_date" data-dat="2021-12-30" data-dat-f="30. Dec 2021" data-global-index="32" data-title="30.12.2021" id="d30-12-2021"><span class="bubble">30</span></td>

>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

You can try attrs:

>>> soup.find("td").attrs['data-dat']
2021-12-30

Full example:

# import module
from bs4 import BeautifulSoup

# create data
dom = """<td class="select_date" data-dat="2021-12-30" data-dat-f="30. Dec 2021" data-global-index="32" data-title="30.12.2021" id="d30-12-2021"><span class="bubble">30</span></td>"""
soup = BeautifulSoup(dom, 'html.parser')

# Extract "data-dat" attribute
print(soup.find("td").attrs['data-dat'])
# 2021-12-30
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