import requests
from datetime import datetime
gg = []
now = datetime.now()
current_time = now.strftime("%Y-%m-%dT%H:%M:%S")
url = ("https://sportsbook-sm-distribution-api.nsoft.com/api/v1/events?deliveryPlatformId=3&dataFormat={"default":"object","events":"array","outcomes":"object"}&language={"default":"sr-Latn","events":"sr-Latn","sport":"sr-Latn","category":"sr-Latn","tournament":"sr-Latn","team":"sr-Latn","market":"sr-Latn"}&timezone=Europe/Belgrade&company={}&companyUuid=4dd61a16-9691-4277-9027-8cd05a647844&filter[sportId]=3&filter[from]={}&sort=categoryPosition,categoryName,tournamentPosition,tournamentName,startsAt&offerTemplate=WEB_OVERVIEW&shortProps=1").format(current_time)
response = requests.get(url)
matches = response.json()
print(matches) #This is my json document
我正在嘗試從博彩網站上獲取賠率。我對 python 很陌生,我需要一些幫助。
某種“奇數 id”存盤在鍵“b”中。所以基本上對于這個 json 檔案中的每個匹配項,如果匹配項包含值為 2763 的鍵“b”,我想刮取鍵“g”的值并將其存盤在我的串列“gg”中(鍵“g”中的值是奇怪的我想刮)。但是,如果匹配不包含值為 2763 的鍵“b”,對于該匹配,我只想添加一次“1.00”到串列“gg”。
for match in matches:
mat = matches['data']['events']
for s in range(len(mat)):
o = mat[s]['o']
for element in o:
h = o[element]['h']
for x in h:
if h[x]['b'] == 2763:
gg.append(h[x]['g'])
使用此命令我可以刮掉賠率,但如果匹配沒有 'b':2763(奇怪),我不知道如何將“1.00”附加到 GG
uj5u.com熱心網友回復:
我還沒有驗證你的代碼,但我假設它有效并且你理解它的作用。你需要的是一面旗幟。標志只是一個布林值 ( True/ False),讓我們將其命名為has_2763。你只需要在每次比賽開始時重置它
for match in matches:
mat = matches['data']['events']
for s in range(len(mat)):
has_2763 = False
o = mat[s]['o']
for element in o:
h = o[element]['h']
for x in h:
if h[x]['b'] == 2763:
gg.append(h[x]['g'])
has_2763 = True
if not has_2763:
gg.append("1.00")
uj5u.com熱心網友回復:
從我對你的問題的理解來看。這應該是解決方案。
json = {
'b': 2763,
'g': 'odd number'
}
x = 'b'
y = 2763
gg = []
for key, value in json.items():
if x == key and y == value: # if finds b: 2763
gg.append(json.get('g'))
elif x == key and y != value:
gg.append(1.00)
break
您可以使用理解來減少行數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/381171.html
