Paul Davis
Paul Davis

Reputation: 525

Find the second instance of a particular class using BeautifulSoup

I am using BeautifulSoup4 to find some scores from a website, I have this so far:

home_team = soup.find('div', class_='tname-home').a.text
away_team = soup.find('div', class_='tname-away').a.text
for scores in soup.find_all('span', class_='scoreboard'):
    score_home = scores.text
    score_away = scores.text
    print(home_team, score_home)
    print(away_team, score_away)

Home team and away team are correct and display correctly. But the scores both come from the same class of 'scoreboard' with the score_home being [0] and score_away [1] (ie, home is the first instance of the class and away is the second instance).

But when I use the [1] it pops up as an error. So I am guessing I don't know where that particular array selector goes. This is my first attempt at this type of code so I am very new to this world and I thought I had it. But when I do:

score_away = scores[1].text

I get an error return self.attrs[key]

I looked it up and it seems to say I am doing it right... but clearly I am not.

So I am willing to bend my understanding a bit further to gain the insight I need to solve this :)

Thanks kindly.

The HTML is as follows:

<div class="current-result" id="event_detail_current_result">
<span class="scoreboard">0</span><span>
<span class="scoreboard-divider">-</span>
<span class="scoreboard">1</span></span>
</div>

Upvotes: 1

Views: 985

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195553

To get home/away score, you can use CSS selector with :nth-child(). For example:

from bs4 import BeautifulSoup


txt = '''<div class="match-info">
<div class="current-result" id="event_detail_current_result">
<span class="scoreboard">0</span><span>
<span class="scoreboard-divider">-</span>
<span class="scoreboard">1</span></span>
</div>'''

soup = BeautifulSoup(txt, 'html.parser')


score_home = soup.select_one('.current-result .scoreboard:nth-child(1)').text
score_away = soup.select_one('.current-result .scoreboard:nth-child(2)').text

print(score_home, '-', score_away)

Prints:

0 - 1

If you want to get all scores for all matches, you can do it inside the loop:

for match in soup.select('.match-info'):
    score_home = match.select_one('.current-result .scoreboard:nth-child(1)').text
    score_away = match.select_one('.current-result .scoreboard:nth-child(2)').text
    print(score_home, '-', score_away)

Upvotes: 1

Related Questions