Getting TypeError:运行此代码时,列表索引必须是整数或片,而不是str

2024-06-03 15:14:09 发布

您现在位置:Python中文网/ 问答频道 /正文

import requests
class food:
    meal = requests.get('https://www.themealdb.com/api/json/v1/1/random.php')
    def __init__(self,init_meals):
     self.meals = init_meals
    def get(self):     
     meal = requests.get('https://www.themealdb.com/api/json/v1/1/search.php?s='+self.meals)
     json_meal = meal.json()
     meal_time = (json_meal['meals'][0]['strMeal'])
     return (meal_time)
    def recipe(self):
     meal = requests.get('https://www.themealdb.com/api/json/v1/1/search.php?s='+self.meals)
     json_meal = meal.json()
     recipes = int(json_meal['meals']['0']['strInstructions'])
     return (recipe_time)

运行此代码时,我得到: 文件“/import/linux/home1/jgarc111/final-exam-fall20-jgarc111/food.py”,菜谱第14行 recipes=int(json_膳食['Founds']['0']['strInstructions']) TypeError:列表索引必须是整数或片,而不是str


Tags: httpsselfcomapijsongetinitdef