web-scrapingquerying

How can I use BeautifulSoup for this Website


I managed to build a query tool that iterates through a list of fighter names to obtain their unique url on bestfightodds.com.

However, now I am in conflict regarding how I can iterate a fighter's website to search the closing odds given a certain fighter's opponent.

For example, say I have Jon Jones' unique fighter page - https://www.bestfightodds.com/fighters/Jon-Jones-819, and an opponent (for example we will choose Daniel Cormier). I would like to return a list of Jon Jones' closing odds for the fights: [-310, -192].

The issue is that I don't have an idea as to how I can link together the opponent's html text to find the respective odds.

I tried iterating through the html text, but was not able to link the fighter name to their respective odds.


Solution

  • You can first find all td that says UFC 182: Jones vs. Cormier and such. Then find it's parent which is class with main-row if you see site. Then do normal find_all or select_one to find the text.

    cormier_closing = [td.parent.find_all(class_='moneyline')[2].text for td in soup.find_all('td', string=re.compile('Cormier'))]
    

    OR

    cormier_closing = [td.parent.select_one('td:nth-child(5)').text for td in soup.find_all('td', string=re.compile('Cormier'))]
    

    EDIT:
    using previous_sibling to select elements and text inside

    opponent = 'Daniel Cormier'    # use full name
    closing_2 = []
    for opp_name in soup.find_all('a', string=opponent):
        opp_el = opp_name.find_parent('tr')    # opponent element: use find_all to get values for opponent
        fighter_el = opp_el.previous_sibling   # fighter element
        c2 = fighter_el.find_all(class_='moneyline')[2].text    # using find_all to get second closing
        closing_2.append(c2)    # appending to closing_2