食物营养成分查询

Sure! To get started, you can use the FoodData Central python API which provides a Python interface to the FoodData Central REST API.

Here’s an example code that uses the API to get nutrition facts for a food item:

# Import the necessary libraries for calculating nutrition information
import requests
import json


# Define the food item to search for
def nutrient(food = "apple"):
    url = "https://api.nal.usda.gov/fdc/v1/foods/search"

    querystring = {"query":food,"pageSize":"1","api_key":"CnttBawbjZXoJl0JGf75LY2Xq3gGPzMCgdRakKZN"}
    response = requests.request("GET", url, params=querystring)

    # Parse the JSON response to get the food item's nutrition information
    food_nutrition = response.json()["foods"][0]["foodNutrients"]

    # Print the nutrition information for the food item
    #print(food_nutrition)
    print(type(food_nutrition))


    with open('data.txt','a+',encoding="utf-8") as f:
        f.write(food)
        f.write('\n\n')
        print(food)
        for nutrient in food_nutrition:
            f.write(f"{nutrient['nutrientName']},{nutrient['value']},{nutrient['unitName']}")
            f.write("\n")
            print(f"{nutrient['nutrientName']},{nutrient['value']},{nutrient['unitName']}")


foods = ['apple','banana','grape','bread']
for food in foods:
    nutrient(food)

apple
Calcium, Ca,0.0,MG
Iron, Fe,0.23,MG
Sodium, Na,0.0,MG
Vitamin A, IU,65.0,IU
Vitamin C, total ascorbic acid,3.1,MG
Cholesterol,0.0,MG
Fatty acids, total saturated,0.0,G
Protein,0.0,G
Carbohydrate, by difference,14.3,G
Energy,52.0,KCAL
Sugars, total including NLEA,10.4,G
Fiber, total dietary,3.2,G
Potassium, K,110,MG
Fatty acids, total trans,0.0,G
Total lipid (fat),0.65,G

banana
Protein,12.5,G
Total lipid (fat),6.25,G
Carbohydrate, by difference,40.6,G
Energy,312,KCAL
Sugars, total including NLEA,6.25,G
Fiber, total dietary,6.2,G
Calcium, Ca,125,MG
Iron, Fe,1.12,MG
Sodium, Na,594,MG
Vitamin A, IU,0.0,IU
Vitamin C, total ascorbic acid,15.0,MG
Cholesterol,0.0,MG
Fatty acids, total trans,0.0,G
Fatty acids, total saturated,0.0,G
Fatty acids, total monounsaturated,3.12,G
Fatty acids, total polyunsaturated,3.12,G

grape
Protein,0.0,G
Total lipid (fat),0.0,G
Carbohydrate, by difference,13.2,G
Energy,54.0,KCAL
Sugars, total including NLEA,13.2,G
Sodium, Na,3.0,MG
Vitamin C, total ascorbic acid,20.3,MG

bread
Protein,3.57,G
Total lipid (fat),0.0,G
Carbohydrate, by difference,32.1,G
Energy,146,KCAL
Sugars, total including NLEA,0.0,G
Fiber, total dietary,3.6,G
Calcium, Ca,0.0,MG
Iron, Fe,0.0,MG
Sodium, Na,354,MG
Vitamin A, IU,0.0,IU
Vitamin C, total ascorbic acid,0.0,MG
Cholesterol,0.0,MG
Fatty acids, total trans,0.0,G
Fatty acids, total saturated,0.0,G

This code will print out the nutrition facts for an apple. You can replace “apple” with any other food item you want to get nutrition facts for.

To use this code, you need to sign up for an API key from the FoodData Central website. Once you have an API key, replace “YOUR_API_KEY” with your actual API key.

I hope this helps! Let me know if you have any questions.