Home > Back-end >  Get text from id in html
Get text from id in html

Time:05-17

i got the below html code

<div >
    <div  id="market_140" onclick="getMarketAccordian('market_140')">
        <div ><span>Second Half Goals – odd/even</span></div>
    </div>
    <div >
        <div >
            <ul >
                <li>
    <button  individualevent-odds-incdec='N' id='highlightBet_79233205' >
        <div >
            <span >Even</span>            
        </div>
        <div >1.80</div>
    </button>
</li>
<li>
    <button  individualevent-odds-incdec='N' id='highlightBet_79233206' >
        <div >
            <span >Odd</span>            
        </div>
        <div >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

CodePudding user response:

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
  • Related