Issue
i got the below html code
<div class="SB-marketBox SB-accordion ">
<div class="SB-marketBox-header SB-accordion-header SB-arrowAfter" id="market_140" onclick="getMarketAccordian('market_140')">
<div class="SB-marketName"><span>Second Half Goals – odd/even</span></div>
</div>
<div class="SB-accordion-content">
<div class="SB-marketBox-content">
<ul class="SB-marketOutComeOddsList ">
<li>
<button class="btn SB-btnOdds SB-btnOutComeOdds " individualevent-odds-incdec='N' id='highlightBet_79233205' >
<div class="SB-outcome-container">
<span class="SB-outcome">Even</span>
</div>
<div class="SB-odds">1.80</div>
</button>
</li>
<li>
<button class="btn SB-btnOdds SB-btnOutComeOdds " individualevent-odds-incdec='N' id='highlightBet_79233206' >
<div class="SB-outcome-container">
<span class="SB-outcome">Odd</span>
</div>
<div class="SB-odds">1.90</div>
</button>
</li>
</ul>
</div>
</div>
</div>
i want to extract the id for Even and Odd outcome have written a python script below to solve it
item = soup.find(class_='SB-marketBox SB-accordion')
Even = item.find_all(class_='SB-odds')[0].get_text()
Odd = item.find_all(class_='SB-odds')[1].get_text()
Even_id = item.find_all(id)[0].get_text()
Odd_id = item.find_all(id)[1].get_text()
print(f'{Even} {Even_id} {Odd} {Odd_id}')
Am getting IndexError: list index out of range. How can I modify the code to get the id for odd and even
Solution
Try:
btn_even = soup.select_one('button:has(span:-soup-contains("Even"))')
btn_odd = soup.select_one('button:has(span:-soup-contains("Odd"))')
print(btn_even["id"], btn_even.select_one(".SB-odds").text)
print(btn_odd["id"], btn_odd.select_one(".SB-odds").text)
Prints:
highlightBet_79233205 1.80
highlightBet_79233206 1.90
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.