简体   繁体   中英

Python error 'list indices must be integers, not str' when trying to pull from api

Line that is producing the error: self.response.write(json_data['results']['title'])

Using the api: http://www.recipepuppy.com/api/?q=

json_data['results'] is a list of dicts, not a dict. The error message

'list indices must be integers, not str' 

is telling you that lists, such as json_data['resuls'] , are indexed by integers, not strings. It's raising an exception since 'title' is not an integer.

So you would need

json_data['results'][0]['title'] to access the first dict in the list.

Since there are many items in the list, to get all the titles separated by \\n s, use

self.response.write('\n'.join([dct['title'] for dct in json_data['results']]))

To see more clearly what the JSON looks like, print json.dumps(..., indent=4) :

In [82]: import json
In [84]: import urllib2    
In [85]: response = urllib2.urlopen('http://www.recipepuppy.com/api/?q=')    
In [87]: content = response.read()
In [91]: print(json.dumps(json.loads(content), indent=4))
{
    "href": "http://www.recipepuppy.com/", 
    "version": 0.1, 
    "results": [   # <-- This bracket tells you the value is a list
        {   # <-- The brace tells you this is a dict or set
            "thumbnail": "http://img.recipepuppy.com/1.jpg",  # <-- The colon tells you this is a dict, not a set
            "href": "http://allrecipes.com/Recipe/Ginger-Champagne/Detail.aspx", 
            "ingredients": "champagne, ginger, ice, vodka", 
            "title": "Ginger Champagne"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/2.jpg", 
            "href": "http://allrecipes.com/Recipe/Potato-and-Cheese-Frittata/Detail.aspx", 
            "ingredients": "cheddar cheese, eggs, olive oil, onions, potato, salt", 
            "title": "Potato and Cheese Frittata"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/3.jpg", 
            "href": "http://allrecipes.com/Recipe/Eggnog-Thumbprints/Detail.aspx", 
            "ingredients": "brown sugar, butter, butter, powdered sugar, eggs, flour, nutmeg, rum, salt, vanilla extract, sugar", 
            "title": "Eggnog Thumbprints"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/4.jpg", 
            "href": "http://allrecipes.com/Recipe/Succulent-Pork-Roast/Detail.aspx", 
            "ingredients": "brown sugar, garlic, pork chops, water", 
            "title": "Succulent Pork Roast"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/5.jpg", 
            "href": "http://allrecipes.com/Recipe/Irish-Champ/Detail.aspx", 
            "ingredients": "black pepper, butter, green onion, milk, potato, salt", 
            "title": "Irish Champ"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/6.jpg", 
            "href": "http://allrecipes.com/Recipe/Chocolate-Cherry-Thumbprints/Detail.aspx", 
            "ingredients": "cocoa powder, baking powder, butter, eggs, flour, oats, salt, sugar, vanilla extract", 
            "title": "Chocolate-Cherry Thumbprints"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/7.jpg", 
            "href": "http://allrecipes.com/Recipe/Mean-Woman-Pasta/Detail.aspx", 
            "ingredients": "garlic, kalamata olive, olive oil, pepperoncini, seashell pasta, tomato", 
            "title": "Mean Woman Pasta"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/8.jpg", 
            "href": "http://allrecipes.com/Recipe/Hot-Spiced-Cider/Detail.aspx", 
            "ingredients": "allspice, apple cider, brown sugar, cinnamon, cloves, nutmeg, orange, salt", 
            "title": "Hot Spiced Cider"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/9.jpg", 
            "href": "http://allrecipes.com/Recipe/Isas-Cola-de-Mono/Detail.aspx", 
            "ingredients": "cinnamon, cloves, instant coffee, milk, rum, vanilla extract, water, sugar", 
            "title": "Isa's Cola de Mono"
        }, 
        {
            "thumbnail": "http://img.recipepuppy.com/10.jpg", 
            "href": "http://allrecipes.com/Recipe/Amys-Barbecue-Chicken-Salad/Detail.aspx", 
            "ingredients": "barbecue sauce, chicken, cilantro, lettuce, ranch dressing, lettuce, tomato", 
            "title": "Amy's Barbecue Chicken Salad"
        }
    ], 
    "title": "Recipe Puppy"
}

The indentation makes it easy to see that json_data['results'] is a list of dicts.

In [92]: json_data = json.loads(content)    

In [94]: print('\n'.join([dct['title'] for dct in json_data['results']]))
Ginger Champagne
Potato and Cheese Frittata
Eggnog Thumbprints
Succulent Pork Roast
Irish Champ
Chocolate-Cherry Thumbprints
Mean Woman Pasta
Hot Spiced Cider
Isa's Cola de Mono
Amy's Barbecue Chicken Salad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM