I am trying to write a script with Node and Cheerio JS that checks a website and returns true if tickets are available for a certain teams game. The basic HTML structure of the site is
<body>
<div class="competition">
<div class="match">
<div class="team-info">
<h3>
<span class="team">Team 1</span>
<span class="team">Team 2</span>
</h3>
</div>
<div class="buy-info">
<div class="btn available">Buy</div>
</div>
</div>
<div class="match">
<div class="team-info">
<h3>
<span class="team">Team 3</span>
<span class="team">Team 4</span>
</h3>
</div>
<div class="buy-info">
<div class="btn available">Buy</div>
</div>
</div>
</div>
</body>
I want to use Cheerio and with an each() function, check each match class and return true of 'Team 1' is the text value of the team span and the corresponding Buy button has the class available. I have written the below so far but on my real life example, it is returning the number of total available buttons * the amount of games 'Team 1' play.
const teamFinder = ".team";
// set match day class
const match = ".match";
let MatchCount = 0;
$(match).each((MDIdx, MDElm) => {
$(teamFinder).each((parentIdx, parentElm) => {
let team = $(parentElm).text();
if (team == "Team 1") {
if ($(buttonDiv).children().hasClass("available")) {
MatchCount++;
}
}
});
});