Home > front end >  extract an element from the class with Beautiful Soup
extract an element from the class with Beautiful Soup

Time:12-30

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

<td  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 >30</span></td>

CodePudding user response:

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  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 >30</span></td>"""
soup = BeautifulSoup(dom, 'html.parser')

# Extract "data-dat" attribute
print(soup.find("td").attrs['data-dat'])
# 2021-12-30
  • Related