外部循环会根据marketid抓取多个URL。每个URL都包含有关赛马的信息。此信息包括赔率。我想在每场比赛中附加最大的赔率。例如,如果赛马的赔率是$ 2.5,$ 3.5,$ 10,$ 36和$ 101,我想附加$ 101。我的问题是,当我将赔率附加到“数据”时,所有URL的所有赔率都将附加。这意味着它将找到所有种族而不是每个种族的最大值。如果内部循环紧随外部循环,则不会发生。
import requests
import datetime
import pandas as pd
startdate = datetime.date(2020, 0o1, 0o1)
enddate = datetime.date(2020, 0o1, 0o2)
daycount = (enddate - startdate).days
date = startdate
marketids = []
for i in range(daycount):
date += datetime.timedelta(days=1)
request1 = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date='+str(date))
json1 = request1.json()
for market in json1:
if market['raceType'] in ['R']:
for id in market['markets']:
marketids.append(market["markets"][0]["marketId"])
data = []
for marketid in marketids:
request2 = requests.get(f'https://betfair-data-supplier-prod.herokuapp.com/api/race_results/?market_id=1.{marketid}&nz_tote_event_id=',
headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
for runner in json2['runners']:
for market in runner['markets']:
if market.get('productType') == 'WIN_ODDS_BSP':
data.append(market.get('price'))
max = 0
for d in data:
if d>max:
max = d
print(max)
这是json的示例:
"runners":[
{
"selectionId":"24369070",
"runnerName":"Bloomin Crafty",
"barrierNo":3,
"saddleClothNo":1,
"placedResult":2,
"startingPosition":1,
"trainerName":"Kathryn Durden",
"jockeyName":"Jarrod Fry",
"weight":58.0,
"silkColor":"Hot Pink Royal Blue Horseshoe Hot Pink Royal Blue \u0026 White Checked Sleeves \u0026 Cap",
"colorURL":"c20200713MURT/1364572.png",
"bestTote":10.4,
"markets":[
{
"productType":"WIN_ODDS_BSP",
"price":10.09,
"reductionFactor":8.33
},
{
"productType":"PLACE_ODDS_BSP",
"price":2.92,
"reductionFactor":13.85
}
],
"bestNZTote":0
}