PASCAL MAGONA
PASCAL MAGONA

Reputation: 107

get attributes of a html class

i got the code below

h = """<div class="SB-kickOffInfo">                                                
                 <div class="SB-kickOff">
                  <div class="SB-kickOff" data-eventdatetime='05/17/2022 18:45:00'></div>
                 </div>
                </div>"""

soup = BeautifulSoup(h)
#print(soup)
kick_off = soup.find(class_="SB-kickOffInfo").get('data-eventdatetime')
print(kick_off)

i want to extract the date but fro the code above am getting None, what should i change to extract the date?

Upvotes: 0

Views: 197

Answers (1)

HedgeHog
HedgeHog

Reputation: 25048

Issue here is that the selected element do not have this attribute you are looking for directly, it is one of its children:

soup.find(class_="SB-kickOffInfo").find(attrs={"data-eventdatetime": True}).get('data-eventdatetime')

Here also a solution with css selectors:

soup.select_one('.SB-kickOffInfo [data-eventdatetime]').get('data-eventdatetime')

Output:

05/17/2022 18:45:00

Upvotes: 1

Related Questions