【问题标题】:How to parse betting odds data from api to create pandas dataframe?如何解析来自 api 的投注赔率数据以创建 pandas 数据框?
【发布时间】:2020-06-20 06:34:32
【问题描述】:

我是编码新手,刚刚学习 api 和 json。我希望创建一个看起来像以下输出的 pandas 数据框:

Name            Over        Line      Under

Al Horford      -125         13        -103

Andrew Wiggins  -130         20        +100

etc.

我可以抓取所有玩家的名字:

import requests
import pandas as pd

url = 'https://betbuilder.digitalsportstech.com/api/feed'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}

payload = {
    'betType': 'in,18,19',
    'gameId': 'in,114109,114110,114111,114112,114113,114114',
    'isActive': '1',
    'limit': '9999',
    'sb': 'betonline',
    'tz': '-8'}

jsonData = requests.get(url, headers=headers, params=payload).json()

players_names = []

def get_names():
    data = jsonData['data']
    for d in data:
        name = d['player1']['name']
        print(name)
        players_names.append(d)

get_names()

但是,我无法找到大小盘投注数据。

我正在寻找 4 个数据来源:

1) 姓名:我能够通过上面的输出找到玩家的姓名。

2) 结束:我找不到这个数据。

3) 行:我能够在标题“值”下找到此数据。

4) 下:我找不到此数据。

总之,我正在寻求帮助来定位 Over 和 Under 数据。然后我想将此数据与相应的名称数据和行数据结合起来。任何帮助将不胜感激。提前感谢您的宝贵时间!

【问题讨论】:

  • 看起来这些字段不是在 API 对象中返回的。您将不得不在其他地方找到它们

标签: python json pandas api parsing


【解决方案1】:

Under 和 Over 数据看起来就在那里。它在odds 键中,由betType 定义。 betType = 18UnderbetType = 19Over

import pandas as pd
import json
from pandas.io.json import json_normalize
import requests
import numpy as np



headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}


# Get Game IDs
url = 'https://betbuilder.digitalsportstech.com/api/latestGames'
payload = {
        'leagueId': '123',
        'order': 'asc',
        'sb': 'betonline',
        'sort': 'date',
        'status': 'in,1,2,3',
        'tz': '0'}

jsonData = requests.get(url, headers=headers, params=payload).json()
gameIds = []
for game in jsonData['data']:
    gameIds.append(str(game['id']))
gameIds = ','.join(gameIds)




# Get Player Data
url = 'https://betbuilder.digitalsportstech.com/api/feed'
payload = {
    'betType': 'in,18,19',
    'gameId': 'in,%s' %gameIds,
    'isActive': '1',
    'limit': '9999',
    'sb': 'betonline',
    'tz': '-8'}

jsonData = requests.get(url, headers=headers, params=payload).json()
if len(jsonData['data']) == 0:
    raise ValueError("O/U Markets for this league will be available as soon as possible\nCheck back soon")


def flatten_df(data):
    finalOutput = pd.DataFrame()
    try:
        data = json_normalize(data)
        print ('Parsing the data...')
    except:
        data=data

    def unpack(temp_df, item):
        try:
            temp_df = json_normalize(item)
            return temp_df
        except:
            pass

    def iterrate(temp_df, row):
        for col, item in row.iteritems():
            if type(item) != list and type(item) != dict or col == 'statistic.phraseTitle':
                if len([ x for x in temp_df.columns if col.startswith(x) ]) > 0:
                        col = col + '_%s' %(len([ x for x in temp_df.columns if col.startswith(x) ]))
                try:
                    temp_df.loc[0,col] = item
                except:
                    temp_df.loc[0,col] = ', '.join(item)
                temp_df = temp_df.ffill()

            elif type(item) == list or type(item) == dict:
                temp_dfa = unpack(temp_df, item)
                temp_dfa.columns = col + '_' + temp_dfa.columns
                idxList = list(temp_df.index.values)
                alpha = temp_dfa.copy()
                merge_df = pd.DataFrame()
                for idx in idxList:
                    alpha.index = [idx]*len(alpha)
                    merge_df = merge_df.append(alpha, sort=False)


                temp_df = pd.merge(temp_df, merge_df, how='outer', left_index=True, right_index=True)
                temp_df = temp_df.reset_index(drop=True)
        return temp_df

    for idx, row in data.iterrows():
        temp_df = pd.DataFrame()
        finalOutput = finalOutput.append(iterrate(temp_df, row),sort=False).reset_index(drop=True)

    continueUnpacking = False
    for idx, row in finalOutput.iterrows():
        if len([ x for x, y in row.iteritems() if type(y) == list or type(y) == dict ]) > 0:
            print ('Still untangling...')
            continueUnpacking = True
            break

    if continueUnpacking == True:
        finalOutput = flatten_df(finalOutput)  


    return finalOutput

results = flatten_df(jsonData['data']) 




playersData = {}
for idx, row in results.iterrows():
    if row['player1.name'] not in list(playersData.keys()):
        playersData[row['player1.name']] = {}

    if row['statistic.title'] not in list(playersData[row['player1.name']].keys()):
        playersData[row['player1.name']][row['statistic.title']] = {'Line': row['markets_value']}

    # Under
    if row['betType'] == 18:
        playersData[row['player1.name']][row['statistic.title']].update({'Under': row['markets_odds']})

    # Over
    if row['betType'] == 19:
        playersData[row['player1.name']][row['statistic.title']].update({'Over': row['markets_odds']})

df = pd.DataFrame()
for player, v in playersData.items():
    temp_df = pd.DataFrame(v).T.reset_index().rename(columns={'index':'statistic.title'})
    temp_df['Name'] = player
    temp_df = temp_df[['Name', 'statistic.title', 'Under', 'Line', 'Over']]
    df = df.append(temp_df, sort=False).reset_index(drop=True)

输出:

这是网站,以及匹配的输出数据:

print (df)
               Name               statistic.title  Under  Line  Over
0   Devonte' Graham               Pts + Reb + Ast   1.90  28.0  1.86
1   Devonte' Graham  Three Point Field Goals Made   1.67   2.5  2.15
2   Devonte' Graham                        Points   1.86  17.5  1.90
3   Devonte' Graham                       Assists   1.65   6.5  2.18
4     Miles Bridges               Pts + Reb + Ast   1.88  24.0  1.88
5     Miles Bridges  Three Point Field Goals Made   1.85   1.5  1.91
6     Miles Bridges                Total Rebounds   1.88   6.0  1.88
7     Miles Bridges                        Points   1.81  15.5  1.96
8        Trae Young                       Assists   1.70   9.5  2.10
9        Trae Young  Three Point Field Goals Made   2.15   3.5  1.67
10       Trae Young               Pts + Reb + Ast   1.87  41.5  1.89
11       Trae Young                        Points   1.89  27.5  1.87
12    Kevin Huerter                        Points   1.92  13.5  1.84
13    Kevin Huerter  Three Point Field Goals Made   2.04   2.5  1.74
14    Kevin Huerter               Pts + Reb + Ast   1.85  21.5  1.91
15     Terry Rozier                        Points   1.95  19.5  1.81
16     Terry Rozier               Pts + Reb + Ast   1.88  27.5  1.88
17     Terry Rozier                Total Rebounds   2.24   4.5  1.62
18     Terry Rozier  Three Point Field Goals Made   1.95   2.5  1.81
19      Cody Zeller                        Points   1.77  10.5  2.00
20      Cody Zeller                Total Rebounds   1.74   6.5  2.05
21      Cody Zeller               Pts + Reb + Ast   1.82  19.5  1.94
22     John Collins                Total Rebounds   1.77   9.5  2.00
23     John Collins  Three Point Field Goals Made   2.12   1.5  1.69
24     John Collins               Pts + Reb + Ast   1.85  34.5  1.91
25     John Collins                        Points   1.86  22.5  1.90
26  P.J. Washington  Three Point Field Goals Made   1.88   1.5  1.88
27  P.J. Washington                Total Rebounds   1.91   5.5  1.85
28  P.J. Washington               Pts + Reb + Ast   1.80  21.5  1.97
29  P.J. Washington                        Points   1.84  13.5  1.92

【讨论】:

  • 非常感谢@chitown88!这远远超出了我目前的理解。我会从中学到很多东西,对此我非常感激。 =)
猜你喜欢
  • 2012-04-04
  • 1970-01-01
  • 2017-12-24
  • 2014-09-28
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多