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!