PYTHON: INTERACTING WITH API 1. I am trying to request data from an API url(Note: Im using a public API for this example
Posted: Thu May 26, 2022 9:23 am
PYTHON: INTERACTING WITH API
1. I am trying to request data from an API url(Note: Im using a
public API for this example but the one for my actual project
requires an API key)
2. I am then trying to extract specific keys from that data and
print their values.
3. Problem: I cannot extract these keys from the data set
requested from the API.
Code:
next_API =
requests.get("https://www.boredapi.com/api/activity")
data2 = next_API.text
stuff = json.loads(data2)
parse = stuff['activity']['price'] #From here I would
like to extract the 'price' and 'activity' keys from the data set
and print their values
print(parse)
Output:
TypeError: string indices must be integers
How can I fix this?
1. I am trying to request data from an API url(Note: Im using a
public API for this example but the one for my actual project
requires an API key)
2. I am then trying to extract specific keys from that data and
print their values.
3. Problem: I cannot extract these keys from the data set
requested from the API.
Code:
next_API =
requests.get("https://www.boredapi.com/api/activity")
data2 = next_API.text
stuff = json.loads(data2)
parse = stuff['activity']['price'] #From here I would
like to extract the 'price' and 'activity' keys from the data set
and print their values
print(parse)
Output:
TypeError: string indices must be integers
How can I fix this?