0

scraping an API for NBA Player Props. It's a nested json where I filtered for my desired output.

import json
from urllib.request import urlopen
import csv
import os

# Delete CSV
os.remove('Path')


jsonurl = urlopen('https://sportsbook.draftkings.com//sites/US-SB/api/v4/eventgroups/88670846/categories/583/subcategories/5001')


games = json.loads(jsonurl.read())['eventGroup']['offerCategories'][8]['offerSubcategoryDescriptors'][0]['offerSubcategory']['offers']

# Open a new file as our CSV file
with open('Path', "a", newline='', encoding='utf-8') as csv_file:
    csv_writer = csv.writer(csv_file)

# Add a header
    csv_writer.writerow(["participant", "line", "oddsDecimal"])

    for game in games:
            for in_game in game:
                outcomes = in_game.get('outcomes')
                for outcome in outcomes:
                    # Write CSV
                    csv_writer.writerow([
                    outcome["participant"],
                    outcome["line"],
                    outcome["oddsDecimal"]
                    
                    ])
                                    

So my issue is here that I have index "8" at "offerCategories" hardcoded in my code (also "0" at "Sub") and it's dynamically changing from day to day at this provider. Not familiar with this stuff but can't figure out how to query this through string with "name": "Player Combos" (index=8 in this example)

Thx in advice!

Joaggi
  • 9
  • 1
  • Take a look at [this example](https://stackoverflow.com/questions/19868404/how-to-get-an-object-inside-an-array-list-by-its-property-value-in-python) of a similar issue. Because the offerCategories object is a list, you cannot access by the value of one of the variables, but you can grab the object where that value matches, then continue to process as you need to. Unfortunately your datasource isn't configured very well, which makes your life difficult, but there isn't much you can do about that! – Stitt Dec 26 '22 at 03:09

1 Answers1

0

Given the data structure you have, you need to iterate through the 'offerCategories' sub-list looking for the map with the appropriate 'name' key. It seems that your use of '0' as an index is fine, since there is only a single value to choose in that case:

import json
from urllib.request import urlopen

jsonurl = urlopen(
    'https://sportsbook.draftkings.com//sites/US-SB/api/v4/eventgroups/88670846/categories/583/subcategories/5001')

games = json.loads(jsonurl.read())

for offerCategory in games['eventGroup']['offerCategories']:
    if offerCategory['name'] == 'Player Combos':
        offers = offerCategory['offerSubcategoryDescriptors'][0]['offerSubcategory']['offers']
        print(offers)
        break

Result:

[[{'providerOfferId': '129944623', 'providerId': 2, 'providerEventId': '27323732', 'providerEventGroupId': '42648', 'label': 'Brandon Clarke Points + Assists + Rebounds...
CryptoFool
  • 21,719
  • 5
  • 26
  • 44