{"id":3253,"date":"2023-04-06T10:56:38","date_gmt":"2023-04-06T02:56:38","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3253"},"modified":"2025-12-16T22:39:55","modified_gmt":"2025-12-16T14:39:55","slug":"%e9%a3%9f%e7%89%a9%e8%90%a5%e5%85%bb%e6%88%90%e5%88%86%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3253","title":{"rendered":"\u98df\u7269\u8425\u517b\u6210\u5206\u67e5\u8be2"},"content":{"rendered":"\n<p>Sure! To get started, you can use the FoodData Central python API which provides a Python interface to the FoodData Central REST API.<\/p>\n\n\n\n<p>Here\u2019s an example code that uses the API to get nutrition facts for a food item:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Import the necessary libraries for calculating nutrition information\nimport requests\nimport json\n\n\n# Define the food item to search for\ndef nutrient(food = \"apple\"):\n    url = \"https:\/\/api.nal.usda.gov\/fdc\/v1\/foods\/search\"\n\n    querystring = {\"query\":food,\"pageSize\":\"1\",\"api_key\":\"Cn_liutz_ttBawbjZXoJl0JGf75LY2Xq3gGPzMCgdRakKZN\"}\n    response = requests.request(\"GET\", url, params=querystring)\n\n    # Parse the JSON response to get the food item's nutrition information\n    food_nutrition = response.json()&#91;\"foods\"]&#91;0]&#91;\"foodNutrients\"]\n\n    # Print the nutrition information for the food item\n    #print(food_nutrition)\n    print(type(food_nutrition))\n\n\n    with open('data.txt','a+',encoding=\"utf-8\") as f:\n        f.write(food)\n        f.write('\\n\\n')\n        print(food)\n        for nutrient in food_nutrition:\n            f.write(f\"{nutrient&#91;'nutrientName']},{nutrient&#91;'value']},{nutrient&#91;'unitName']}\")\n            f.write(\"\\n\")\n            print(f\"{nutrient&#91;'nutrientName']},{nutrient&#91;'value']},{nutrient&#91;'unitName']}\")\n\n\nfoods = &#91;'apple','banana','grape','bread']\nfor food in foods:\n    nutrient(food)\n<\/code><\/pre>\n\n\n\n<p>apple<br>Calcium, Ca,0.0,MG<br>Iron, Fe,0.23,MG<br>Sodium, Na,0.0,MG<br>Vitamin A, IU,65.0,IU<br>Vitamin C, total ascorbic acid,3.1,MG<br>Cholesterol,0.0,MG<br>Fatty acids, total saturated,0.0,G<br>Protein,0.0,G<br>Carbohydrate, by difference,14.3,G<br>Energy,52.0,KCAL<br>Sugars, total including NLEA,10.4,G<br>Fiber, total dietary,3.2,G<br>Potassium, K,110,MG<br>Fatty acids, total trans,0.0,G<br>Total lipid (fat),0.65,G<br><br>banana<br>Protein,12.5,G<br>Total lipid (fat),6.25,G<br>Carbohydrate, by difference,40.6,G<br>Energy,312,KCAL<br>Sugars, total including NLEA,6.25,G<br>Fiber, total dietary,6.2,G<br>Calcium, Ca,125,MG<br>Iron, Fe,1.12,MG<br>Sodium, Na,594,MG<br>Vitamin A, IU,0.0,IU<br>Vitamin C, total ascorbic acid,15.0,MG<br>Cholesterol,0.0,MG<br>Fatty acids, total trans,0.0,G<br>Fatty acids, total saturated,0.0,G<br>Fatty acids, total monounsaturated,3.12,G<br>Fatty acids, total polyunsaturated,3.12,G<br><br>grape<br>Protein,0.0,G<br>Total lipid (fat),0.0,G<br>Carbohydrate, by difference,13.2,G<br>Energy,54.0,KCAL<br>Sugars, total including NLEA,13.2,G<br>Sodium, Na,3.0,MG<br>Vitamin C, total ascorbic acid,20.3,MG<br><br>bread<br>Protein,3.57,G<br>Total lipid (fat),0.0,G<br>Carbohydrate, by difference,32.1,G<br>Energy,146,KCAL<br>Sugars, total including NLEA,0.0,G<br>Fiber, total dietary,3.6,G<br>Calcium, Ca,0.0,MG<br>Iron, Fe,0.0,MG<br>Sodium, Na,354,MG<br>Vitamin A, IU,0.0,IU<br>Vitamin C, total ascorbic acid,0.0,MG<br>Cholesterol,0.0,MG<br>Fatty acids, total trans,0.0,G<br>Fatty acids, total saturated,0.0,G<\/p>\n\n\n\n<p>This code will print out the nutrition facts for an apple. You can replace \u201capple\u201d with any other food item you want to get nutrition facts for.<\/p>\n\n\n\n<p>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 \u201cYOUR_API_KEY\u201d with your actual API key.<\/p>\n\n\n\n<p>I hope this helps! Let me know if you have any questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sure! To get started, you can use the Fo <span class=\"readmore\"><a href=\"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3253\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,1],"tags":[],"class_list":["post-3253","post","type-post","status-publish","format-standard","hentry","category-python","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3253"}],"version-history":[{"count":4,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3253\/revisions"}],"predecessor-version":[{"id":6760,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3253\/revisions\/6760"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3253"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}