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
I am trying to scrape odds from betting site. I am pretty new to python and I need some help.
Some kind of "Odd id" is stored in key "b". So basically for every match in this json file, if match contains key "b" with value 2763, i want to scrape value of key "g" and store it in my list "gg"(in key "g" value is odd that i want to scrape). But if match doesn't contains key "b" with value 2763, for that match I want to append only one time "1.00" to list "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'])
With this command i can scrape odds but i don't how to append "1.00" to GG if match doesn't have 'b':2763 (oddid)
CodePudding user response:
i haven't verified your code but i'm assuming it works and you understand what it does. what you need is a flag. a flag is just a boolean (True
/False
), let's name it has_2763
. you just have to reset it at each match start
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")
CodePudding user response:
From what I can understand from your problem. This should be the solution.
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
you can reduce the number of lines using comprehension.