KeyError when parsing json when key is there - python

I have the code below which collects data from an API. I am then parsing and collecting the data points I need. I am having an issue where I am getting a KeyError but the key exists and all the data is returned, so if there is an error, why does it work?
any help would be appreciated.
url = f"{Base_url}/betoffer/event/{','.join(map(str, event_ids))}.json?app_id={AppID}&app_key={AppKey}&local={Country}&site={Site}&type=125"
print(url)
player_td_data = requests.get(url).text
player_td_data = json.loads(player_td_data)
for td_data in player_td_data['betOffers']:
if td_data['criterion']['label'] == 'First Touchdown Scorer - Including Overtime' and 'OFFERED_PREMATCH' in td_data['tags']:
for outcomes in td_data['outcomes']:
if outcomes['type'] == 'OT_PLAYER_PARTICIPANT':
event_id = td_data['eventId']
player_id = outcomes['id']
player_name = outcomes['participant']
player_first_td_odds = outcomes['odds']
print(f'{event_id} {player_id} {player_name} {player_first_td_odds}')
which produces the data i am required below
1018663065 3172232074 Wentz, Carson 23000
1018663065 3172232100 McKissic, JD 17000
1018663065 3172232109 Gibson, Antonio 13000
1018663065 3172232117 Robinson Jr., Brian 8500
1018663065 3172232126 Griffin, Ryan 51000
1018663065 3172232134 Pettis, Dante 31000
1018663065 3172232140 St. Brown, Equanimeous 21000
1018663065 3172232146 Kmet, Cole 16000
1018663065 3172232153 Harry, N’Keal 31000
1018663065 3172232159 Fields, Justin 9000
1018663065 3172232164 Montgomery, David 6750
1018663065 3172232170 Blasingame, Khari 76000
1018663065 3172232176 Wesco, Trevon 76000
1018663065 3172232181 Smith-Marsette, Ihmir 61000
1018663065 3172232186 Ebner, Trestan 56000
1018663065 3172232190 Mooney, Darnell 12000
1018663065 3172232194 Herbert, Khalil 16000
1018663065 3172232197 Tonges, Jake 91000
1018663065 3172232200 Jones Jr., Velus 34000
1018663065 3172232259 Samuel, Curtis 10500
1018663065 3172232265 Sims, Cam 41000
1018663065 3172232268 McLaurin, Terry 9000
but it also gives a KeyError below
Traceback (most recent call last):
File "C:\Users\xx\OneDrive\Desktop\xxx-functions\testing.py", line 87, in <module>
first_touchdowns_odds(event_ids)
File "C:\Users\xxx\OneDrive\Desktop\xxx-functions\testing.py", line 77, in first_touchdowns_odds
player_first_td_odds = outcomes['odds']
KeyError: 'odds'
this is the json
{
"betOffers": [{
"eventId": 1018663065,
"criterion": {
"englishLabel": "First Touchdown Scorer - Including Overtime",
"id": 1001488021,
"label": "First Touchdown Scorer - Including Overtime",
"order": [
1
]
},
"tags": [
"OFFERED_PREMATCH",
"BET_BUILDER"
],
"outcomes": [{
"oddsFractional": "22/1",
"englishLabel": "Wentz, Carson",
"label": "Wentz, Carson",
"betOfferId": 2349921831,
"oddsAmerican": "2200",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Wentz, Carson",
"participantId": 1003128420,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 23000,
"cashOutStatus": "ENABLED",
"id": 3172232074,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "50/1",
"englishLabel": "No Touchdown",
"label": "No Touchdown",
"betOfferId": 2349921831,
"oddsAmerican": "5000",
"type": "OT_NO_GOAL",
"changedDate": "2022-10-12T21:37:28Z",
"odds": 51000,
"cashOutStatus": "ENABLED",
"id": 3172232086,
"status": "OPEN"
},
{
"oddsFractional": "16/1",
"englishLabel": "McKissic, JD",
"label": "McKissic, JD",
"betOfferId": 2349921831,
"oddsAmerican": "1600",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "McKissic, JD",
"participantId": 1003405978,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 17000,
"cashOutStatus": "ENABLED",
"id": 3172232100,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "12/1",
"englishLabel": "Gibson, Antonio",
"label": "Gibson, Antonio",
"betOfferId": 2349921831,
"oddsAmerican": "1200",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Gibson, Antonio",
"participantId": 1006035581,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 13000,
"cashOutStatus": "ENABLED",
"id": 3172232109,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "15/2",
"englishLabel": "Robinson Jr., Brian",
"label": "Robinson Jr., Brian",
"betOfferId": 2349921831,
"oddsAmerican": "750",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Robinson Jr., Brian",
"participantId": 1007667637,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 8500,
"cashOutStatus": "ENABLED",
"id": 3172232117,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "50/1",
"englishLabel": "Griffin, Ryan",
"label": "Griffin, Ryan",
"betOfferId": 2349921831,
"oddsAmerican": "5000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Griffin, Ryan",
"participantId": 1002198957,
"changedDate": "2022-10-13T01:18:07Z",
"odds": 51000,
"cashOutStatus": "ENABLED",
"id": 3172232126,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "30/1",
"englishLabel": "Pettis, Dante",
"label": "Pettis, Dante",
"betOfferId": 2349921831,
"oddsAmerican": "3000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Pettis, Dante",
"participantId": 1003741140,
"changedDate": "2022-10-13T01:18:07Z",
"odds": 31000,
"cashOutStatus": "ENABLED",
"id": 3172232134,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "20/1",
"englishLabel": "St. Brown, Equanimeous",
"label": "St. Brown, Equanimeous",
"betOfferId": 2349921831,
"oddsAmerican": "2000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "St. Brown, Equanimeous",
"participantId": 1004561624,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 21000,
"cashOutStatus": "ENABLED",
"id": 3172232140,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "15/1",
"englishLabel": "Kmet, Cole",
"label": "Kmet, Cole",
"betOfferId": 2349921831,
"oddsAmerican": "1500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Kmet, Cole",
"participantId": 1005209906,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 16000,
"cashOutStatus": "ENABLED",
"id": 3172232146,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "30/1",
"englishLabel": "Harry, N’Keal",
"label": "Harry, N’Keal",
"betOfferId": 2349921831,
"oddsAmerican": "3000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Harry, N’Keal",
"participantId": 1005252420,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 31000,
"cashOutStatus": "ENABLED",
"id": 3172232153,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "8/1",
"englishLabel": "Fields, Justin",
"label": "Fields, Justin",
"betOfferId": 2349921831,
"oddsAmerican": "800",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Fields, Justin",
"participantId": 1005289956,
"changedDate": "2022-10-12T21:37:30Z",
"odds": 9000,
"cashOutStatus": "ENABLED",
"id": 3172232159,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "23/4",
"englishLabel": "Montgomery, David",
"label": "Montgomery, David",
"betOfferId": 2349921831,
"oddsAmerican": "575",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Montgomery, David",
"participantId": 1005330489,
"changedDate": "2022-10-13T01:18:07Z",
"odds": 6750,
"cashOutStatus": "ENABLED",
"id": 3172232164,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "75/1",
"englishLabel": "Blasingame, Khari",
"label": "Blasingame, Khari",
"betOfferId": 2349921831,
"oddsAmerican": "7500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Blasingame, Khari",
"participantId": 1005619153,
"changedDate": "2022-10-12T21:37:30Z",
"odds": 76000,
"cashOutStatus": "ENABLED",
"id": 3172232170,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "75/1",
"englishLabel": "Wesco, Trevon",
"label": "Wesco, Trevon",
"betOfferId": 2349921831,
"oddsAmerican": "7500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Wesco, Trevon",
"participantId": 1005620092,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 76000,
"cashOutStatus": "ENABLED",
"id": 3172232176,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "60/1",
"englishLabel": "Smith-Marsette, Ihmir",
"label": "Smith-Marsette, Ihmir",
"betOfferId": 2349921831,
"oddsAmerican": "6000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Smith-Marsette, Ihmir",
"participantId": 1005740936,
"changedDate": "2022-10-13T01:18:07Z",
"odds": 61000,
"cashOutStatus": "ENABLED",
"id": 3172232181,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "55/1",
"englishLabel": "Ebner, Trestan",
"label": "Ebner, Trestan",
"betOfferId": 2349921831,
"oddsAmerican": "5500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Ebner, Trestan",
"participantId": 1005999128,
"changedDate": "2022-10-12T21:37:30Z",
"odds": 56000,
"cashOutStatus": "ENABLED",
"id": 3172232186,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "11/1",
"englishLabel": "Mooney, Darnell",
"label": "Mooney, Darnell",
"betOfferId": 2349921831,
"oddsAmerican": "1100",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Mooney, Darnell",
"participantId": 1006797242,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 12000,
"cashOutStatus": "ENABLED",
"id": 3172232190,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "15/1",
"englishLabel": "Herbert, Khalil",
"label": "Herbert, Khalil",
"betOfferId": 2349921831,
"oddsAmerican": "1500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Herbert, Khalil",
"participantId": 1007051724,
"changedDate": "2022-10-12T21:37:30Z",
"odds": 16000,
"cashOutStatus": "ENABLED",
"id": 3172232194,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "90/1",
"englishLabel": "Tonges, Jake",
"label": "Tonges, Jake",
"betOfferId": 2349921831,
"oddsAmerican": "9000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Tonges, Jake",
"participantId": 1007678491,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 91000,
"cashOutStatus": "ENABLED",
"id": 3172232197,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "34/1",
"englishLabel": "Jones Jr., Velus",
"label": "Jones Jr., Velus",
"betOfferId": 2349921831,
"oddsAmerican": "3400",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Jones Jr., Velus",
"participantId": 1007905969,
"changedDate": "2022-10-13T01:18:07Z",
"odds": 35000,
"cashOutStatus": "ENABLED",
"id": 3172232200,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "17/1",
"englishLabel": "Any other Chicago Bears player",
"label": "Any other Chicago Bears player",
"betOfferId": 2349921831,
"oddsAmerican": "1700",
"type": "OT_ANY_OTHER_PARTICIPANT",
"participant": "Chicago Bears",
"participantId": 1000000178,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 18000,
"cashOutStatus": "ENABLED",
"id": 3172232205,
"status": "OPEN",
"eventParticipantId": 1000000178
},
{
"oddsFractional": "19/2",
"englishLabel": "Samuel, Curtis",
"label": "Samuel, Curtis",
"betOfferId": 2349921831,
"oddsAmerican": "950",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Samuel, Curtis",
"participantId": 1002450777,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 10500,
"cashOutStatus": "ENABLED",
"id": 3172232259,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "40/1",
"englishLabel": "Sims, Cam",
"label": "Sims, Cam",
"betOfferId": 2349921831,
"oddsAmerican": "4000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Sims, Cam",
"participantId": 1003050339,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 41000,
"cashOutStatus": "ENABLED",
"id": 3172232265,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "8/1",
"englishLabel": "McLaurin, Terry",
"label": "McLaurin, Terry",
"betOfferId": 2349921831,
"oddsAmerican": "800",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "McLaurin, Terry",
"participantId": 1003741186,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 9000,
"cashOutStatus": "ENABLED",
"id": 3172232268,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"englishLabel": "Thomas, Logan",
"label": "Thomas, Logan",
"betOfferId": 2349921831,
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Thomas, Logan",
"participantId": 1004231988,
"changedDate": "2022-10-12T21:09:17Z",
"cashOutStatus": "ENABLED",
"id": 3172232271,
"status": "SUSPENDED",
"eventParticipantId": 1000000334
},
{
"englishLabel": "Dotson, Jahan",
"label": "Dotson, Jahan",
"betOfferId": 2349921831,
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Dotson, Jahan",
"participantId": 1005740692,
"changedDate": "2022-10-12T21:09:17Z",
"cashOutStatus": "ENABLED",
"id": 3172232274,
"status": "SUSPENDED",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "80/1",
"englishLabel": "Milne, Dax",
"label": "Milne, Dax",
"betOfferId": 2349921831,
"oddsAmerican": "8000",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Milne, Dax",
"participantId": 1007029029,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 81000,
"cashOutStatus": "ENABLED",
"id": 3172232277,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "19/1",
"englishLabel": "Brown, Dyami",
"label": "Brown, Dyami",
"betOfferId": 2349921831,
"oddsAmerican": "1900",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Brown, Dyami",
"participantId": 1007094475,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 20000,
"cashOutStatus": "ENABLED",
"id": 3172232279,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "45/1",
"englishLabel": "Turner, Cole",
"label": "Turner, Cole",
"betOfferId": 2349921831,
"oddsAmerican": "4500",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Turner, Cole",
"participantId": 1007180703,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 46000,
"cashOutStatus": "ENABLED",
"id": 3172232281,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "66/1",
"englishLabel": "Rogers, Armani",
"label": "Rogers, Armani",
"betOfferId": 2349921831,
"oddsAmerican": "6600",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Rogers, Armani",
"participantId": 1007800362,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 67000,
"cashOutStatus": "ENABLED",
"id": 3172232283,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "18/1",
"englishLabel": "Bates, John",
"label": "Bates, John",
"betOfferId": 2349921831,
"oddsAmerican": "1800",
"type": "OT_PLAYER_PARTICIPANT",
"participant": "Bates, John",
"participantId": 1007957637,
"changedDate": "2022-10-12T22:13:43Z",
"odds": 19000,
"cashOutStatus": "ENABLED",
"id": 3172232285,
"status": "OPEN",
"eventParticipantId": 1000000334
},
{
"oddsFractional": "16/1",
"englishLabel": "Any other Washington Commanders player",
"label": "Any other Washington Commanders player",
"betOfferId": 2349921831,
"oddsAmerican": "1600",
"type": "OT_ANY_OTHER_PARTICIPANT",
"participant": "Washington Commanders",
"participantId": 1000000334,
"changedDate": "2022-10-12T21:37:28Z",
"odds": 17000,
"cashOutStatus": "ENABLED",
"id": 3172232289,
"status": "OPEN",
"eventParticipantId": 1000000334
}
],
"cashOutStatus": "ENABLED",
"closed": "2022-10-14T00:15:00Z",
"betOfferType": {
"englishName": "Player Occurrence Number",
"name": "Player Occurrence Number",
"id": 125
},
"id": 2349921831
}],
"range": {
"total": 1,
"size": 1,
"start": 0
},
"events": [{
"englishName": "Chicago Bears - Washington Commanders",
"groupId": 1000093656,
"homeName": "Chicago Bears",
"groupSortOrder": 2876820019684212700,
"prematchEnd": "2022-10-14T00:15:00Z",
"path": [{
"englishName": "American Football",
"termKey": "american_football",
"name": "American Football",
"id": 1000093199
},
{
"englishName": "NFL",
"termKey": "nfl",
"name": "NFL",
"id": 1000093656
}
],
"id": 1018663065,
"state": "NOT_STARTED",
"group": "NFL",
"start": "2022-10-14T00:15:00Z",
"nonLiveBoCount": 254,
"tags": [
"OFFERED_LIVE",
"BET_BUILDER",
"MATCH",
"LIVE_OCCURRENCE_FEED"
],
"awayName": "Washington Commanders",
"name": "Chicago Bears - Washington Commanders",
"sport": "AMERICAN_FOOTBALL"
}]
}

One small modification to your code allows you to see which record is causing the error.
for td_data in player_td_data['betOffers']:
if td_data['criterion']['label'] == 'First Touchdown Scorer - Including Overtime' and 'OFFERED_PREMATCH' in td_data['tags']:
for outcomes in td_data['outcomes']:
if outcomes['type'] == 'OT_PLAYER_PARTICIPANT':
event_id = td_data['eventId']
player_id = outcomes['id']
player_name = outcomes['participant']
try:
player_first_td_odds = outcomes['odds']
except KeyError:
print(f'KeyError on {player_name}')
break
print(f'{event_id} {player_id} {player_name} {player_first_td_odds}')
You can see that indeed the JSON record for Thomas, Logan is missing the odds field.

Related

Using Pandas for JSON to Excel conversion is putting the entire JSON file in 1 Excel Cell

I am making a call to the Google Places Autocomplete API which is returning data in a JSON formatted browser tab.
I use Beautiful Soup to get this data. I then write it to a file and get an unexpected output from pandas.
newUrl = webUrl+'json?input='+searchString+'&offset=0'+'&components=country:us&key='+apiKey
li = r"C:/Users/thebr/iCloudDrive/code/python/captones/capstoneData.json"
r = requests.get(newUrl)
html_page = r.content
soup = BeautifulSoup(html_page, 'html.parser')
newDictionary = json.loads(str(soup))
out_file = open("python/captones/nydata.json", "w")
json.dump(newDictionary, out_file)
out_file.close()
df = pd.read_json("python/captones/nydata.json")
df.to_excel('output.xlsx', index=False)
My output puts the entire JSON file into 1 cell. Do I have to add some parameters?
EDIT: Here's an example JSON that the API provides:
{
"predictions": [{
"description": "COVID-19 vaccine location - Stony Brook University, Nicolls Road, Stony Brook, NY, USA",
"matched_substrings": [{
"length": 5,
"offset": 0
}],
"place_id": "ChIJw-RIqTo_6IkRnW9u_u9x1b8",
"reference": "ChIJw-RIqTo_6IkRnW9u_u9x1b8",
"structured_formatting": {
"main_text": "COVID-19 vaccine location - Stony Brook University",
"main_text_matched_substrings": [{
"length": 5,
"offset": 0
}],
"secondary_text": "Nicolls Road, Stony Brook, NY, USA"
},
"terms": [{
"offset": 0,
"value": "COVID-19 vaccine location - Stony Brook University"
}, {
"offset": 52,
"value": "Nicolls Road"
}, {
"offset": 66,
"value": "Stony Brook"
}, {
"offset": 79,
"value": "NY"
}, {
"offset": 83,
"value": "USA"
}],
"types": ["health", "point_of_interest", "establishment"]
}, {
"description": "COVID-19 Vaccine Location - Meadowlands Racing & Entertainment, Racetrack Dr, East Rutherford, NJ, USA",
"matched_substrings": [{
"length": 5,
"offset": 0
}],
"place_id": "ChIJ5TThhWP4wokRQ9TLrhSPQRQ",
"reference": "ChIJ5TThhWP4wokRQ9TLrhSPQRQ",
"structured_formatting": {
"main_text": "COVID-19 Vaccine Location - Meadowlands Racing & Entertainment",
"main_text_matched_substrings": [{
"length": 5,
"offset": 0
}],
"secondary_text": "Racetrack Dr, East Rutherford, NJ, USA"
},
"terms": [{
"offset": 0,
"value": "COVID-19 Vaccine Location - Meadowlands Racing & Entertainment"
}, {
"offset": 64,
"value": "Racetrack Dr"
}, {
"offset": 78,
"value": "East Rutherford"
}, {
"offset": 95,
"value": "NJ"
}, {
"offset": 99,
"value": "USA"
}],
"types": ["health", "point_of_interest", "establishment"]
}, {
"description": "COVID-19 Drive-thru Testing at Walgreens, New York 112, Medford, NY, USA",
"matched_substrings": [{
"length": 5,
"offset": 0
}],
"place_id": "ChIJVVUVMLpI6IkRb0XzqEXttHQ",
"reference": "ChIJVVUVMLpI6IkRb0XzqEXttHQ",
"structured_formatting": {
"main_text": "COVID-19 Drive-thru Testing at Walgreens",
"main_text_matched_substrings": [{
"length": 5,
"offset": 0
}],
"secondary_text": "New York 112, Medford, NY, USA"
},
"terms": [{
"offset": 0,
"value": "COVID-19 Drive-thru Testing at Walgreens"
}, {
"offset": 42,
"value": "New York 112"
}, {
"offset": 56,
"value": "Medford"
}, {
"offset": 65,
"value": "NY"
}, {
"offset": 69,
"value": "USA"
}],
"types": ["pharmacy", "health", "point_of_interest", "store", "establishment"]
}, {
"description": "COVID-19 Vaccine Location - Medgar Evers College, Crown Street, Brooklyn, NY, USA",
"matched_substrings": [{
"length": 5,
"offset": 0
}],
"place_id": "ChIJqQOySnFbwokRcormV9Q7bvA",
"reference": "ChIJqQOySnFbwokRcormV9Q7bvA",
"structured_formatting": {
"main_text": "COVID-19 Vaccine Location - Medgar Evers College",
"main_text_matched_substrings": [{
"length": 5,
"offset": 0
}],
"secondary_text": "Crown Street, Brooklyn, NY, USA"
},
"terms": [{
"offset": 0,
"value": "COVID-19 Vaccine Location - Medgar Evers College"
}, {
"offset": 50,
"value": "Crown Street"
}, {
"offset": 64,
"value": "Brooklyn"
}, {
"offset": 74,
"value": "NY"
}, {
"offset": 78,
"value": "USA"
}],
"types": ["health", "establishment"]
}, {
"description": "COVID-19 Drive-Thru Testing at Walgreens, West Main Street, Patchogue, NY, USA",
"matched_substrings": [{
"length": 5,
"offset": 0
}],
"place_id": "ChIJVVVlOxpJ6IkRDcNMmDf-qi8",
"reference": "ChIJVVVlOxpJ6IkRDcNMmDf-qi8",
"structured_formatting": {
"main_text": "COVID-19 Drive-Thru Testing at Walgreens",
"main_text_matched_substrings": [{
"length": 5,
"offset": 0
}],
"secondary_text": "West Main Street, Patchogue, NY, USA"
},
"terms": [{
"offset": 0,
"value": "COVID-19 Drive-Thru Testing at Walgreens"
}, {
"offset": 42,
"value": "West Main Street"
}, {
"offset": 60,
"value": "Patchogue"
}, {
"offset": 71,
"value": "NY"
}, {
"offset": 75,
"value": "USA"
}],
"types": ["health", "point_of_interest", "establishment"]
}],
"status": "OK"
}
Also to add, this is how the output looks after converting to excel:
And this is how it looks using an online json to excel Converter (What I want):
It can help you select only the predictions object and then reading it with pandas. To flatten out more the output you will need additional work. You can look here for an example Conversion from nested json to csv with pandas
import json
import pandas as pd
dic = json.load("python/captones/nydata.json")
dic_predictions = dic["predictions"]
df = pd.read_json(dic_predictions)
df.to_excel('output.xlsx', index=False)

(Python) Cant scrape data from my targeted site anymore using re, requests, and json

I'm having a problem where i can scrape data from a website by using the java pathing. I'm trying to scrape from Rocket League Tracker.
here's my code:
import requests
import re
import json
import math
def rankGetter():
trackerLink = 'https://rocketleague.tracker.network/rocket-league/profile/epic/DirectPanda/overview'
# now we have the tracker link we're going to scrape the website
# all the HTML of the site is now in result
result = requests.get(trackerLink)
# checker to make sure the user used the correct information
if result.status_code == 400:
print('profile not found')
else:
# Extract everything needed to render the current page. Data is stored as Json in the
# JavaScript variable: window.__INITIAL_STATE__={"route":{"path":"\u0 ... }};
json_string = re.search(r"window.__INITIAL_STATE__\s?=\s?(\{.*?\});", result.text).group(1)
# convert text string to structured json data
rocketleague = json.loads(json_string)
# Save structured json data to a text file that helps you orient yourself and pick
# the parts you are interested in.
with open('rocketleague_json_data.txt', 'w') as outfile:
outfile.write(json.dumps(rocketleague, indent=4, sort_keys=True))
The error is the text doc made doesn't have the ranks I want anymore.
"stats": {
"standardLeaderboardLeaders": {},
"standardLeaderboards": [],
"standardPlayers": {},
"standardTitles": {}
},
**"stats-v2": {
"segments": {},
"standardProfileMatches": {},
"standardProfileSummaries": {},
"standardProfiles": {},
"standardProfilesHistory": {},
"standardSessions": {},
"subscriptions": {}
},**
"titles": {
"currentTitle": {
"name": "Rocket League",
"platforms": [
The Ranks should be under stats-V2 but as you can see its empty now.
whats happening and how do i fix it? I was able to get ranks for a week but all the sudden it stopped working today.
Seems that the data are loaded from external URL:
import json
import requests
url = "https://api.tracker.gg/api/v2/rocket-league/standard/profile/epic/DirectPanda"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0"
}
data = requests.get(url, headers=headers).json()
print(json.dumps(data, indent=4))
Prints:
{
"data": {
"platformInfo": {
"platformSlug": "epic",
"platformUserId": null,
"platformUserHandle": "DirectPanda",
"platformUserIdentifier": "DirectPanda",
"avatarUrl": null,
"additionalParameters": null
},
"userInfo": {
"userId": null,
"isPremium": false,
"isVerified": false,
"isInfluencer": false,
"isPartner": false,
"countryCode": null,
"customAvatarUrl": null,
"customHeroUrl": null,
"socialAccounts": [],
"pageviews": 592,
"isSuspicious": null
},
"metadata": {
"lastUpdated": {
"value": "2021-04-22T17:39:42.277-04:00",
"displayValue": "2021-04-22T21:39:42.2770000+00:00"
},
"playerId": 16603481,
"currentSeason": 17
},
"segments": [
{
"type": "overview",
"attributes": {},
"metadata": {
"name": "Lifetime"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"wins": {
"rank": 30357,
"percentile": 98.3,
"displayName": "Wins",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 4985,
"displayValue": "4,985",
"displayType": "Number"
},
"goals": {
"rank": 23698,
"percentile": 98.7,
"displayName": "Goals",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 14363,
"displayValue": "14,363",
"displayType": "Number"
},
"mVPs": {
"rank": 35646,
"percentile": 98.0,
"displayName": "MVPs",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 2093,
"displayValue": "2,093",
"displayType": "Number"
},
"saves": {
"rank": 30864,
"percentile": 98.3,
"displayName": "Saves",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 9231,
"displayValue": "9,231",
"displayType": "Number"
},
"assists": {
"rank": 29228,
"percentile": 98.4,
"displayName": "Assists",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 4763,
"displayValue": "4,763",
"displayType": "Number"
},
"shots": {
"rank": 24596,
"percentile": 98.6,
"displayName": "Shots",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 29139,
"displayValue": "29,139",
"displayType": "Number"
},
"goalShotRatio": {
"rank": 1409320,
"percentile": 15.0,
"displayName": "Goal Shot Ratio",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 49.29132777377398,
"displayValue": "49.3",
"displayType": "NumberPrecision1"
},
"score": {
"rank": 28260,
"percentile": 98.4,
"displayName": "TRN Score",
"displayCategory": "General",
"category": "general",
"metadata": {},
"value": 2398222.83,
"displayValue": "2,398,222.8",
"displayType": "NumberPrecision1"
},
"seasonRewardLevel": {
"rank": null,
"percentile": 85.0,
"displayName": "Season Reward Level",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-13.png",
"rankName": "Diamond"
},
"value": 5,
"displayValue": "5",
"displayType": "Number"
},
"seasonRewardWins": {
"rank": null,
"percentile": 95.8,
"displayName": "Season Reward Wins",
"displayCategory": "General",
"category": "general",
"metadata": {},
"value": 9,
"displayValue": "9",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 0,
"season": 17
},
"metadata": {
"name": "Un-Ranked"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-0.png",
"name": "Unranked"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"name": "Division I"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": null,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": null,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "win"
},
"value": 0,
"displayValue": "0",
"displayType": "Number"
},
"rating": {
"rank": 215152,
"percentile": 90.0,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1597,
"displayValue": "1,597",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 10,
"season": 17
},
"metadata": {
"name": "Ranked Duel 1v1"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 98.2,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-16.png",
"name": "Champion I"
},
"value": 16,
"displayValue": "16",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 88.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 13,
"deltaUp": 6,
"name": "Division III"
},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 57.0,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 60.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "win"
},
"value": 1,
"displayValue": "1",
"displayType": "Number"
},
"rating": {
"rank": 101541,
"percentile": 96.1,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1031,
"displayValue": "1,031",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 11,
"season": 17
},
"metadata": {
"name": "Ranked Doubles 2v2"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 87.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-16.png",
"name": "Champion I"
},
"value": 16,
"displayValue": "16",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 90.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 15,
"deltaUp": 3,
"name": "Division IV"
},
"value": 3,
"displayValue": "3",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 80.0,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 40,
"displayValue": "40",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 34.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "loss"
},
"value": 1,
"displayValue": "-1",
"displayType": "Number"
},
"rating": {
"rank": 311789,
"percentile": 89.0,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1177,
"displayValue": "1,177",
"displayType": "Number"
}
}
},
{
"type": "playlist",
"attributes": {
"playlistId": 13,
"season": 17
},
"metadata": {
"name": "Ranked Standard 3v3"
},
"expiryDate": "0001-01-01T00:00:00+00:00",
"stats": {
"tier": {
"rank": null,
"percentile": 96.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"iconUrl": "https://trackercdn.com/cdn/tracker.gg/rocket-league/ranks/s4-17.png",
"name": "Champion II"
},
"value": 17,
"displayValue": "17",
"displayType": "Number"
},
"division": {
"rank": null,
"percentile": 79.0,
"displayName": "Matches",
"displayCategory": "General",
"category": "general",
"metadata": {
"deltaDown": 7,
"deltaUp": 27,
"name": "Division III"
},
"value": 2,
"displayValue": "2",
"displayType": "Number"
},
"matchesPlayed": {
"rank": null,
"percentile": 97.8,
"displayName": "Matches",
"displayCategory": "Performance",
"category": "performance",
"metadata": {},
"value": 95,
"displayValue": "95",
"displayType": "Number"
},
"winStreak": {
"rank": null,
"percentile": 16.0,
"displayName": "WinStreak",
"displayCategory": "Performance",
"category": "performance",
"metadata": {
"type": "loss"
},
"value": 2,
"displayValue": "-2",
"displayType": "Number"
},
"rating": {
"rank": 122500,
"percentile": 95.8,
"displayName": "Rating",
"displayCategory": "Skill",
"category": "skill",
"metadata": {},
"value": 1255,
"displayValue": "1,255",
"displayType": "Number"
}
}
},
...

How to join document in search query

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.2876821,
"hits": [
{
"_index": "product_index",
"_type": "product",
"_id": "1115",
"_score": 0.2876821,
"_source": {
"isactive": true,
"in_use": false,
"brand_name": "Adidas",
"sku_id": "56456487987987",
"long_description": "this is long description",
"key_feature": [
{
"id": 1148,
"key_feature": "sport wear"
},
{
"id": 1147,
"key_feature": "Cotton shirt"
},
{
"id": 1146,
"key_feature": "White and blue"
}
],
"isdeleted": false,
"created_by": null,
"brand_id": 5,
"search_terms": [
{
"label": "white shirt",
"value": 9
}
]
"color_id": 2,
"specific_keywords": "",
"item_list": [
{
"item_id": 1114,
"product_id": 1115,
"isactive": true,
"id": 9,
"isdeleted": false
},
{
"item_id": 1113,
"product_id": 1115,
"isactive": true,
"id": 10,
"isdeleted": false
}
],
"upc_code": "",
"display_size": "L",
"name": "New White shirt",
"updated_by": null,
"id": 1115,
"updated_date": "2020-03-25T08:24:37.644571+00:00",
"color_name": "blue",
"created_date": "2020-03-25T08:11:14.966673+00:00",
"category": [
{
"parent_category_id": 78,
"sub_sub_category": null,
"sub_category": null,
"sub_category_id": null,
"sub_sub_category_id": null,
"parent_category": "new Shirt Cate",
"id": 1151
}
]
}
},
{
"_index": "product_index",
"_type": "product",
"_id": "1113",
"_score": 0.2876821,
"_source": {
"isactive": true,
"in_use": false,
"sku_id": "1456456488",
"brand_name": "Adidas",
"long_description": "",
"key_feature": [
{
"id": 1142,
"key_feature": "Cotton"
},
{
"id": 1141,
"key_feature": "Office Use"
},
{
"id": 1140,
"key_feature": "Black formal"
}
],
"isdeleted": false,
"created_by": null,
"brand_id": 5,
"search_terms": [
]
"color_id": 1,
"specific_keywords": "",
"item_list": [
],
"display_size": "L",
"upc_code": "",
"name": "New Cotton formal shirt black",
"updated_by": null,
"id": 1113,
"updated_date": "2020-03-25T06:48:30.903041+00:00",
"created_date": "2020-03-25T06:48:29.943043+00:00",
"color_name": "black",
"category": [
{
"sub_sub_category": null,
"parent_category_id": 54,
"sub_category": null,
"sub_category_id": null,
"sub_sub_category_id": null,
"parent_category": "MEN'S CLOTHING",
"id": 1149
}
]
}
},
{
"_index": "product_index",
"_type": "product",
"_id": "1114",
"_score": 0.2876821,
"_source": {
"isactive": true,
"in_use": false,
"sku_id": "145645648811",
"brand_name": "Adidas",
"long_description": "",
"key_feature": [
{
"id": 1145,
"key_feature": "Cotton"
},
{
"id": 1144,
"key_feature": "Office Use"
},
{
"id": 1143,
"key_feature": "Black formal"
}
],
"isdeleted": false,
"created_by": null,
"brand_id": 5,
"search_terms": [
],
"color_id": 1,
"specific_keywords": "",
"item_list": [
],
"display_size": "L",
"upc_code": "",
"updated_by": null,
"name": "New Cotton Casual shirt black",
"id": 1114,
"created_date": "2020-03-25T07:13:26.233675+00:00",
"color_name": "black",
"updated_date": "2020-03-25T07:13:27.229363+00:00",
"category": [
{
"sub_sub_category": null,
"parent_category_id": 54,
"sub_category": null,
"sub_category_id": null,
"sub_sub_category_id": null,
"parent_category": "MEN'S CLOTHING",
"id": 1150
}
]
}
}
]
}
}
my requirement is to attach all related documents with specific key value fields which is specify in item_list based on item_id. In above result doc id 1115 has item_list which contains item_id 1114 and 1113. so the particular fields attach in the doc 1115.
what should be the search query for that in elastic search?
You can't do join in Elasticsearch, to achieve your goal, you can do two things:
duplicate the information of item_id 1114 and 1113 in the item_id
1115 (and for sure in all others documents).
Do join at application level, so after this query you can extract the item_id 1114 and 1113 and run two others query to get the information about this items. Then join all the json at application level.

Synonym analyzer not working in elastic search with python

I have a scenario as depicted below in python code .
In this I am trying to explicitly define new york and ny as synonyms. But unfortunately it is not working. Can you please guide me as I am new to elastic search.
Also I am using custom analyzer.
I also have the file synonyms.txt having text:
ny,newyork,nyork
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
keywords = ['thousand eyes', 'facebook', 'superdoc', 'quora', 'your story', 'Surgery', 'lending club', 'ad roll',
'the honest company', 'Draft kings', 'newyork']
count = 1
doc_setting = {
"settings": {
"analysis": {
"analyzer": {
"my_analyzer_keyword": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
},
"my_analyzer_shingle": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "synonyms.txt",
"ignore_case": "true"
}
}
}
}, "mappings": {
"your_type": {
"properties": {
"keyword": {
"type": "string",
"index_analyzer": "my_analyzer_keyword",
"search_analyzer": "my_analyzer_shingle"
}
}
}
}
}
validate=es.index(index='test', doc_type='your_type', body=doc_setting)
print(validate)
for keyword in keywords:
doc = {
'id': count,
'keyword': keyword
}
res = es.index(index="test", doc_type='your_type', id=count, body=doc)
print(res['result'])
count = count + 1
#res11 = es.get(index="test", doc_type='your_type', id=1)
#print(res11['_source'])
es.indices.refresh(index="test")
question = "I saw news on ny news channel of lending club on facebook, your story and quora"
print("Question asked: %s" % question)
res = es.search(index="test",`enter code here` doc_type='your_type', body={
"query": {"match": {"keyword": question}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print(hit["_source"])
PUT /test_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer_keyword": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
},
"my_analyzer_shingle": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
}
},
"filter": {
"synonym" : {
"type" : "synonym",
"lenient": true,
"synonyms" : ["ny,newyork,nyork"]
}
}
}
}, "mappings": {
"your_type": {
"properties": {
"keyword": {
"type": "text",
"analyzer": "my_analyzer_keyword",
"search_analyzer": "my_analyzer_shingle"
}
}
}
}
}
Then Analyze using
POST /test_index/_analyze
{
"analyzer" : "my_analyzer_shingle",
"text" : "I saw news on ny news channel of lending club on facebook, your story and quorat"
}
The tokens I get are
{
"tokens": [
{
"token": "i",
"start_offset": 0,
"end_offset": 1,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "saw",
"start_offset": 2,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "news",
"start_offset": 6,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "on",
"start_offset": 11,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "ny",
"start_offset": 14,
"end_offset": 16,
"type": "<ALPHANUM>",
"position": 4
},
{
"token": "newyork",
"start_offset": 14,
"end_offset": 16,
"type": "SYNONYM",
"position": 4
},
{
"token": "nyork",
"start_offset": 14,
"end_offset": 16,
"type": "SYNONYM",
"position": 4
},
{
"token": "news",
"start_offset": 17,
"end_offset": 21,
"type": "<ALPHANUM>",
"position": 5
},
{
"token": "channel",
"start_offset": 22,
"end_offset": 29,
"type": "<ALPHANUM>",
"position": 6
},
{
"token": "of",
"start_offset": 30,
"end_offset": 32,
"type": "<ALPHANUM>",
"position": 7
},
{
"token": "lending",
"start_offset": 33,
"end_offset": 40,
"type": "<ALPHANUM>",
"position": 8
},
{
"token": "club",
"start_offset": 41,
"end_offset": 45,
"type": "<ALPHANUM>",
"position": 9
},
{
"token": "on",
"start_offset": 46,
"end_offset": 48,
"type": "<ALPHANUM>",
"position": 10
},
{
"token": "facebook",
"start_offset": 49,
"end_offset": 57,
"type": "<ALPHANUM>",
"position": 11
},
{
"token": "your",
"start_offset": 59,
"end_offset": 63,
"type": "<ALPHANUM>",
"position": 12
},
{
"token": "story",
"start_offset": 64,
"end_offset": 69,
"type": "<ALPHANUM>",
"position": 13
},
{
"token": "and",
"start_offset": 70,
"end_offset": 73,
"type": "<ALPHANUM>",
"position": 14
},
{
"token": "quorat",
"start_offset": 74,
"end_offset": 80,
"type": "<ALPHANUM>",
"position": 15
}
]
}
and the search produces
POST /test_index/_search
{
"query" : {
"match" : { "keyword" : "I saw news on ny news channel of lending club on facebook, your story and quora" }
}
}
{
"took": 36,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.6858001,
"hits": [
{
"_index": "test_index",
"_type": "your_type",
"_id": "4",
"_score": 1.6858001,
"_source": {
"keyword": "newyork"
}
},
{
"_index": "test_index",
"_type": "your_type",
"_id": "2",
"_score": 1.1727304,
"_source": {
"keyword": "facebook"
}
},
{
"_index": "test_index",
"_type": "your_type",
"_id": "5",
"_score": 0.6931472,
"_source": {
"keyword": "quora"
}
}
]
}
}

Getting Sections of an API using requests

I am trying to get a section of the fortnite API that is in lifeTimeStats and the kills value. I can't figure out how to just get the kills and not the whole API.
import requests
headers = {'TRN-Api-Key': '52f2fbde-018d-47b5-9c7d-99990c0d3507'}
url = 'https://api.fortnitetracker.com/v1/profile/pc/loopisjones'
r = requests.get(url, headers = headers)
test = r.text
print test
These are the results there are too many character to enter the whole thing but I want to just get the kills from the "lifeTimeStats":
{
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"platformId": 3,
"platformName": "pc",
"platformNameLong": "PC",
"epicUserHandle": "loopisjones",
"stats": {
"p2": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1111,
"value": "1111",
"rank": 2507075,
"displayValue": "1,111"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 97533,
"value": "97533",
"rank": 279372,
"displayValue": "97,533"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 3,
"value": "3",
"rank": 978112,
"percentile": 48.0,
"displayValue": "3"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 56,
"value": "56",
"rank": 577434,
"percentile": 6.0,
"displayValue": "56"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 176,
"value": "176",
"rank": 318099,
"percentile": 3.2,
"displayValue": "176"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.04,
"value": "1.04",
"rank": 1484862,
"percentile": 42.0,
"displayValue": "1.04"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 0.5,
"value": "0.5",
"rank": 1549361,
"percentile": 61.0,
"displayValue": "0.50"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 658,
"value": "658",
"rank": 277505,
"percentile": 12.0,
"displayValue": "658"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 680,
"value": "680",
"rank": 470461,
"percentile": 18.0,
"displayValue": "680"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1484405,
"percentile": 41.0,
"displayValue": "1.03"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 323.43,
"value": "323.43",
"rank": 1535727,
"displayValue": "5m 23s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 148.23,
"value": "148.23",
"rank": 1272686,
"percentile": 53.0,
"displayValue": "148.23"
}
},
"p10": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1200,
"value": "1200",
"displayValue": "1,200"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 46764,
"value": "46764",
"rank": 1123263,
"displayValue": "46,764"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 3,
"value": "3",
"rank": 1383465,
"percentile": 48.0,
"displayValue": "3"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 20,
"value": "20",
"rank": 1438657,
"percentile": 15.0,
"displayValue": "20"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 56,
"value": "56",
"rank": 1335530,
"percentile": 14.0,
"displayValue": "56"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.68,
"value": "0.68",
"rank": 2133720,
"percentile": 68.0,
"displayValue": "0.68"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 0.9,
"value": "0.9",
"rank": 1752091,
"percentile": 55.0,
"displayValue": "0.90"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 347,
"value": "347",
"rank": 944115,
"percentile": 30.0,
"displayValue": "347"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 234,
"value": "234",
"rank": 1291960,
"percentile": 40.0,
"displayValue": "234"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.67,
"value": "0.67",
"rank": 2137868,
"percentile": 68.0,
"displayValue": "0.67"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 273.72,
"value": "273.72",
"rank": 2125448,
"displayValue": "4m 33s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 134.77,
"value": "134.77",
"rank": 2058752,
"percentile": 63.0,
"displayValue": "134.77"
}
},
"p9": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1121,
"value": "1121",
"rank": 2180620,
"displayValue": "1,121"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 98339,
"value": "98339",
"rank": 880324,
"displayValue": "98,339"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 8,
"value": "8",
"rank": 1488695,
"percentile": 31.0,
"displayValue": "8"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 36,
"value": "36",
"rank": 1240417,
"percentile": 13.0,
"displayValue": "36"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 80,
"value": "80",
"rank": 1113740,
"percentile": 11.0,
"displayValue": "80"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.94,
"value": "0.94",
"rank": 1775409,
"percentile": 54.0,
"displayValue": "0.94"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.3,
"value": "1.3",
"rank": 2230627,
"percentile": 49.0,
"displayValue": "1.30"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 603,
"value": "603",
"rank": 677528,
"percentile": 14.0,
"displayValue": "603"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 562,
"value": "562",
"rank": 873151,
"percentile": 21.0,
"displayValue": "562"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.93,
"value": "0.93",
"rank": 1731964,
"percentile": 53.0,
"displayValue": "0.93"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 221.59,
"value": "221.59",
"rank": 2532805,
"displayValue": "3m 41s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 163.08,
"value": "163.08",
"rank": 2029616,
"percentile": 42.0,
"displayValue": "163.08"
}
},
"curr_p2": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1111,
"value": "1111",
"displayValue": "1,111"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 22500,
"value": "22500",
"rank": 470532,
"percentile": 26.0,
"displayValue": "22,500"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1238303,
"displayValue": "0"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 16,
"value": "16",
"rank": 526184,
"percentile": 6.0,
"displayValue": "16"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 43,
"value": "43",
"rank": 384684,
"percentile": 3.9,
"displayValue": "43"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1459203,
"displayValue": "1.03"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 149,
"value": "149",
"rank": 455652,
"percentile": 19.0,
"displayValue": "149"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 153,
"value": "153",
"rank": 637148,
"percentile": 26.0,
"displayValue": "153"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.03,
"value": "1.03",
"rank": 1446183,
"percentile": 51.0,
"displayValue": "1.03"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 70.87,
"value": "70.87",
"rank": 962042,
"percentile": 39.0,
"displayValue": "1m 10s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 151.01,
"value": "151.01",
"rank": 1366860,
"percentile": 65.0,
"displayValue": "151.01"
}
},
"curr_p10": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1200,
"value": "1200",
"displayValue": "1,200"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 13669,
"value": "13669",
"rank": 1152025,
"percentile": 42.0,
"displayValue": "13,669"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 1,
"value": "1",
"rank": 1151758,
"displayValue": "1"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 7,
"value": "7",
"rank": 1141708,
"percentile": 15.0,
"displayValue": "7"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 22,
"value": "22",
"rank": 903205,
"percentile": 11.0,
"displayValue": "22"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 0.75,
"value": "0.75",
"rank": 1887251,
"displayValue": "0.75"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.0,
"value": "1",
"rank": 1443516,
"percentile": 54.0,
"displayValue": "1.00"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 96,
"value": "96",
"rank": 936423,
"percentile": 35.0,
"displayValue": "96"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 71,
"value": "71",
"rank": 1252315,
"percentile": 46.0,
"displayValue": "71"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 0.74,
"value": "0.74",
"rank": 1893112,
"percentile": 71.0,
"displayValue": "0.74"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 26.88,
"value": "26.88",
"rank": 2497616,
"percentile": 91.0,
"displayValue": "26s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 142.39,
"value": "142.39",
"rank": 1954451,
"percentile": 71.0,
"displayValue": "142.39"
}
},
"curr_p9": {
"trnRating": {
"label": "TRN Rating",
"field": "TRNRating",
"category": "Rating",
"valueInt": 1121,
"value": "1121",
"displayValue": "1,121"
},
"score": {
"label": "Score",
"field": "Score",
"category": "General",
"valueInt": 48923,
"value": "48923",
"rank": 498058,
"percentile": 8.0,
"displayValue": "48,923"
},
"top1": {
"label": "Wins",
"field": "Top1",
"category": "Tops",
"valueInt": 4,
"value": "4",
"rank": 1164590,
"displayValue": "4"
},
"top3": {
"label": "Top 3",
"field": "Top3",
"category": "Tops",
"valueInt": 16,
"value": "16",
"rank": 861117,
"percentile": 11.0,
"displayValue": "16"
},
"top5": {
"label": "Top 5",
"field": "Top5",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top6": {
"label": "Top 6",
"field": "Top6",
"category": "Tops",
"valueInt": 38,
"value": "38",
"rank": 634089,
"percentile": 7.0,
"displayValue": "38"
},
"top10": {
"label": "Top 10",
"field": "Top10",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top12": {
"label": "Top 12",
"field": "Top12",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"top25": {
"label": "Top 25",
"field": "Top25",
"category": "Tops",
"valueInt": 0,
"value": "0",
"rank": 1,
"displayValue": "0"
},
"kd": {
"label": "K/d",
"field": "KD",
"category": "General",
"valueDec": 1.02,
"value": "1.02",
"rank": 1482777,
"displayValue": "1.02"
},
"winRatio": {
"label": "Win %",
"field": "WinRatio",
"category": "General",
"valueDec": 1.2,
"value": "1.2",
"rank": 1953805,
"percentile": 52.0,
"displayValue": "1.20"
},
"matches": {
"label": "Matches",
"field": "Matches",
"category": "General",
"valueInt": 322,
"value": "322",
"rank": 188452,
"percentile": 2.8,
"displayValue": "322"
},
"kills": {
"label": "Kills",
"field": "Kills",
"category": "General",
"valueInt": 324,
"value": "324",
"rank": 455442,
"percentile": 10.0,
"displayValue": "324"
},
"kpg": {
"label": "Kills Per Match",
"field": "KPG",
"category": "General",
"valueDec": 1.01,
"value": "1.01",
"rank": 1435682,
"percentile": 52.0,
"displayValue": "1.01"
},
"avgTimePlayed": {
"label": "Avg Match Time",
"field": "AvgTimePlayed",
"category": "General",
"valueDec": 27.02,
"value": "27.02",
"rank": 2559533,
"percentile": 91.0,
"displayValue": "27s"
},
"scorePerMatch": {
"label": "Score per Match",
"field": "ScorePerMatch",
"category": "General",
"valueDec": 151.93,
"value": "151.93",
"rank": 2317854,
"percentile": 64.0,
"displayValue": "151.93"
}
}
},
"lifeTimeStats": [
{
"key": "Top 3",
"value": "56"
},
{
"key": "Top 5s",
"value": "20"
},
{
"key": "Top 3s",
"value": "36"
},
{
"key": "Top 6s",
"value": "80"
},
{
"key": "Top 12s",
"value": "56"
},
{
"key": "Top 25s",
"value": "176"
},
{
"key": "Score",
"value": "242,636"
},
{
"key": "Matches Played",
"value": "1608"
},
{
"key": "Wins",
"value": "14"
},
{
"key": "Win%",
"value": "1%"
},
{
"key": "Kills",
"value": "1476"
},
{
"key": "K/d",
"value": "0.93"
},
{
"key": "Kills Per Min",
"value": "0.2"
},
{
"key": "Time Played",
"value": "5d 2h 37m "
},
{
"key": "Avg Survival Time",
"value": "4m 34s"
}
],
"recentMatches": [
{
"id": 71608051,
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"playlist": "p2",
"kills": 15,
"minutesPlayed": 0,
"top1": 0,
"top5": 0,
"top6": 0,
"top10": 2,
"top12": 0,
"top25": 4,
"matches": 16,
"top3": 0,
"dateCollected": "2018-03-21T12:26:33.713",
"score": 2018,
"platform": 3,
"trnRating": 1111.8,
"trnRatingChange": -9.6963567258982941
},
{
"id": 71608050,
"accountId": "649eb4a8-584e-4896-adc9-66569ee92139",
"playlist": "p9",
"kills": 7,
"minutesPlayed": 0,
"top1": 0,
"top5": 0,
"top6": 2,
"top10": 0,
"top12": 0,
"top25": 0,
"matches": 11,
"top3": 0,
"dateCollected": "2018-03-21T12:26:33.697",
"score": 1342,
"platform": 3,
"trnRating": 1121.4,
"trnRatingChange": -9.6722971885223643
},
...
You can use the built-in .json() function of the requests module.
headers = {'TRN-Api-Key': '52f2fbde-018d-47b5-9c7d-99990c0d3507'}
url = 'https://api.fortnitetracker.com/v1/profile/pc/loopisjones'
r = requests.get(url, headers=headers)
data = r.json()
for item in data['lifeTimeStats']:
if item['key'] == 'Kills':
print(item['value'])
Output:
1520

Categories