Python Series : Draftkings MLB Batter Props

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Waterstpub87
    SBR MVP
    • 09-09-09
    • 4102

    #1
    Python Series : Draftkings MLB Batter Props
    For installation of python and selenium, please see this post



    The background of this script was my changing from offshore to US books completely for this baseball season. Unfortunately, there is a large difference in the MLB props for offshore sites as compared to onshore sites. Typically, I had bet Hits/Runs/RBIs as a sum, which was the bets offered by the offshores. They offered it as a matchup, typically 2-4 matchups per games, and typically 4 single player over/unders. Similar with total bases. They had hits as well.

    Onshore, it seems to be that the offering is individual players. Needless to say, trying to manually price 18 * 4 props (18 players hits,runs,rbis,bases) is time consuming. I wrote this script to pull in the tables I wanted, and configured excel to read and price them.

    Draftkings prop page is set up into tables, IE 1 table for home run, 1 table for RBIs, ect.

    A note to remember is that python starts at 0 for numbering. So the first table (Home Runs, which would be 0) is ignored.

    This script will pull tables 2, 3, 4 ,5, which are Hits, Bases, RBI's and Runs.



    Below is example output:

    Player Category Line Over Odds Under Odds
    0 Abraham Toro Hits 0.5 -180 130
    0 Adam Frazier Hits 1.5 185 -250
    0 Alex Bregman Hits 0.5 -195 145
    0 Chas McCormick Hits 0.5 -165 120
    It will produce an excel sheet with this information.
  • Waterstpub87
    SBR MVP
    • 09-09-09
    • 4102

    #2
    Code:
    import pandas as pd
    
    from selenium import webdriver
    
    
    
    cols  = ['Player','Category','Line','Over Odds','Under Odds']
    
    props = pd.DataFrame(columns = cols)
    
    driver = webdriver.Chrome('Path to your chrome driver')
    
    driver.get('https://sportsbook.draftkings.com/event/180314630?category=odds&subcategory=batter-props')
    tables = driver.find_elements_by_xpath('//div[@class="sportsbook-event-accordion__wrapper expanded"]')
    
    for x in tables[1:5]:
        eventype = x.find_element_by_xpath('.//a[@aria-current="page"]').text
        rows = x.find_elements_by_xpath('.//tr')
        if eventype == 'Home Runs':
            betype = 'HR'
        elif eventype == 'Hits':
            betype = 'Hits'
            
        elif eventype == 'Total Bases':
            betype = 'TB'
            
        elif eventype == 'RBIs':
            betype = 'RBI'
        elif eventype == 'Runs Scored':
            betype = 'Runs'
        else:
            betype = 'Error'
        
        
        for z in rows[1:]:
            currentbet = pd.DataFrame(columns = cols)
            currentbet.at[0,"Player"] = z.find_element_by_xpath('.//span[@class="sportsbook-row-name"]').text
            currentbet.at[0,"Category"] = betype
            currentbet.at[0,"Line"] = z.find_element_by_xpath('.//span[@class="sportsbook-outcome-cell__line"]').text
            betodds = z.find_elements_by_xpath('.//span[@class="sportsbook-odds american default-color"]')
            
            currentbet.at[0,"Over Odds"] = betodds[0].text
            currentbet.at[0,"Under Odds"] = betodds[1].text
            props = props.append(currentbet)
            
    props.to_csv('MLBpropscrape.csv')    
    driver.close()
    Comment
    • Waterstpub87
      SBR MVP
      • 09-09-09
      • 4102

      #3
      To use this script, you must paste in the url of the game here:
      driver.get('https://sportsbook.draftkings.com/event/180314630?category=odds&subcategory=batt er-props')

      you can go to the draft kings website to find this. Just click on a game on the main page, and you can find the event number.



      Additionally, I take only the 4 tables. If you wanted everything ( so what we have and HRs+Singles+Doubles+Triples + Stolen bases,

      go here :
      for x in tables[1:5]:

      and make it look like this :
      for x in tables:

      I rename some of them, which is the part that looks like this:


      elif eventype == 'Total Bases':
      betype = 'TB'

      if You are pulling in all the tables, the name will become "error" due to the coding. To fix this,

      take this part :
      betype = 'Error'

      and change it to:
      betype = eventype

      This script will output the table to where ever you have the script saved.

      If you want to save it somewhere else, specify it like this:

      directory = 'C\\Users\\Documents\\'
      It must be formated like that, double slashes within single quotes

      change this line:
      props.to_csv('MLBpropscrape.csv')
      to
      props.to_csv(directory + 'MLBpropscrape.csv')
      Comment
      • peacebyinches
        SBR MVP
        • 02-13-10
        • 1112

        #4
        This looks great! Something like this is the kind of tool for streamlining player props (which like you said, can get out of hand fast with the multiple stat categories across multiple players across multiple games) that I could definitely use. Thanks for sharing!
        Comment
        SBR Contests
        Collapse
        Top-Rated US Sportsbooks
        Collapse
        Working...