DrewS
DrewS

Reputation: 93

Use list items in variable in python requests url

I am trying to make a call to an API and then grab event_ids from the data. I then want to use those event ids as variables in another request, then parse that data. Then loop back and make another request using the next event id in the event_id variable for all the IDs.

so far i have the following

def nba_odds():

    url = "https://xxxxx.com.au/sports/summary/basketball?api_key=xxxxx"
    response = requests.get(url)
    data = response.json()

    event_ids = []

    for event in data['Events']:
        if event['Country'] == 'USA' and event['League'] == 'NBA':
            event_ids.append(event['EventID'])

    # print(event_ids)


    game_url = f'https://xxxxx.com.au/sports/detail/{event_ids}?api_key=xxxxx'
    game_response = requests.get(game_url)
    game_data = game_response.json()

    print(game_url)

that gives me the result below in the terminal.

https://xxxxx.com.au/sports/detail/['dbx-1425135', 'dbx-1425133', 'dbx-1425134', 'dbx-1425136', 'dbx-1425137', 'dbx-1425138', 'dbx-1425139', 'dbx-1425140', 'anyvsany-nba01-1670043600000000000', 'dbx-1425141', 'dbx-1425142', 'dbx-1425143', 'dbx-1425144', 'dbx-1425145', 'dbx-1425148', 'dbx-1425149', 'dbx-1425147', 'dbx-1425146', 'dbx-1425150', 'e95270f6-661b-46dc-80b9-cd1af75d38fb', '0c989be7-0802-4683-8bb2-d26569e6dcf9']?api_key=779ac51a-2fff-4ad6-8a3e-6a245a0a4cbb

the URL above format should look like

https://xxxx.com.au/sports/detail/dbx-1425135

If anyone can point me in the right direction it would be appreciated.

thanks.

Upvotes: 0

Views: 504

Answers (2)

Usman Arshad
Usman Arshad

Reputation: 552

you need to loop over the event ID's again to call the API with one event_id if it is not supporting multiple event_ids like:

    all_events_response = []
    for event_id in event_ids
        game_url = f'https://xxxxx.com.au/sports/detail/{event_id}?api_key=xxxxx'
        game_response = requests.get(game_url)
        game_data = game_response.json()
        all_events_response.append(game_data)
        print(game_url)

You can find list of json responses under all_events_response

Upvotes: 1

tdelaney
tdelaney

Reputation: 77337

event_ids is an entire list of event ids. You make a single URL with the full list converted to its string view (['dbx-1425135', 'dbx-1425133', ...]). But it looks like you want to get information on each event in turn. To do that, put the second request in the loop so that it runs for every event you find interesting.

def nba_odds():

    url = "https://xxxxx.com.au/sports/summary/basketball?api_key=xxxxx"
    response = requests.get(url)
    data = response.json()

    event_ids = []

    for event in data['Events']:
        if event['Country'] == 'USA' and event['League'] == 'NBA':
            event_id = event['EventID']
            # print(event_id)
            game_url = f'https://xxxxx.com.au/sports/detail/{event_id}?api_key=xxxxx'
            game_response = requests.get(game_url)
            game_data = game_response.json()
            # do something with game_data - it will be overwritten
            # on next round in the loop
            print(game_url)

Upvotes: 1

Related Questions