Python / IJSON - результат exceptions.keyerror - PullRequest
0 голосов
/ 06 июля 2019

У меня проблемы со ссылкой на изображение в следующем коде (см. Item2):

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    subjects = []

    for content in soup.find_all('item'):

        try:        
            link = content.find('enclosure')
            link = link.get('url')
            print "\n\nLink: ", link

            title = content.find('title')
            title = title.get_text()

            desc = content.find('itunes:subtitle')
            desc = desc.get_text()

#                thumbnail = content.find('itunes:image')
#                thumbnail = thumbnail.get('href')

        except AttributeError:
            continue

        item1 = {
                'url': link,
                'title': title,
                'desc': desc,
#                'thumbnail': thumbnail
        }

    subjects.append(item1) 

    f = urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
    objects = ijson.items(f, 'object.posts.image')
    image = (['type'] == 'medium')
#        for medium in image:
#            do_something_with('image')

    item2 = {
                'image': thumbnail,
        }

    subjects.append(item2) 

    return subjects


def compile_playable_podcast(playable_podcast):
    """
    @para: list containing dict of key/values pairs for playable     podcasts
    """
    items = []

    for podcast in playable_podcast:
        items.append({
            'label': podcast['title'],
            'thumbnail': podcast['thumbnail'],
            'path': podcast['url'],
            'info': podcast['desc'],
            'is_playable': True,
    })

    return items

Я хочу пропустить первое изображение в аргументе item1 и извлечь изображение изСайт json в аргументе item2 с использованием ijson.

Ошибка:

Error Type: <type 'exceptions.KeyError'>
Error Contents: 'thumbnail'  
File "....py", line 92, in compile_playable_podcast
'thumbnail': podcast['thumbnail'],
KeyError: 'thumbnail'
-->End of Python script error report<--

Буду признателен за любой совет.

...