I am using an API that retrieves all the nutritional facts about fruits from the USDA, and posts them on an Excel sheet, but I only get 50 results. I want to export every fruit from their web site. How do I get the whole list?
import requests
import json
import pandas as pd
def call_API(foodName, apiKey):
url = f'https://api.nal.usda.gov/fdc/v1/foods/search?api_key={apiKey}&query={foodName}'
r = requests.get(url)
return r.json()
api_response = call_API('raw', '#API KEY')
# search
table_data = []
for food_item in api_response['foods']:
row = {
"Description": food_item["description"]
}
for nutrient in food_item["foodNutrients"]:
nutrient_name = nutrient["nutrientName"]
row[nutrient_name] = nutrient["value"]
table_data.append(row)
# creating a DataFrame
df = pd.DataFrame(table_data)
print(df.iloc[-1])
df.to_excel('output.xlsx', index=False)
Use the pageNumber as shown below:
If you want to pull page count before looping try: