Set request headers
Many APIs require users provide an API key, obtained by registering for the service. Keys typically are passed in the request header, rather than as parameters.
The Yelp API documentation says "To authenticate API calls with the API Key, set the Authorization HTTP header value as Bearer api_key."
You'll set up a dictionary to pass this information to get(), call the API for the highest-rated cafes in NYC, and parse the response.
pandas (as pd) and requests have been loaded. The API endpoint is stored as api_url, and the key is api_key. Parameters are in the dictionary params.
Deze oefening maakt deel uit van de cursus
Streamlined Data Ingestion with pandas
Oefeninstructies
- Create a dictionary,
headers, that passes the formatted key string to the"Authorization"header value. - Query the Yelp API (
api_url) withget()and the necessary headers and parameters. Save the result asresponse. - Extract the JSON data from
response. Save the result asdata. - Load the
"businesses"values indatato the dataframecafesand print thenamescolumn.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Create dictionary that passes Authorization and key string
headers = {____: "Bearer {}".format(____)}
# Query the Yelp API with headers and params set
response = ____
# Extract JSON data from response
data = ____
# Load "businesses" values to a dataframe and print names
cafes = ____
print(cafes.name)