Python / IJSON-exceptions.keyerror结果

时间:2019-07-06 04:11:20

标签: python parsing beautifulsoup ijson

在以下代码中,我在引用指向图像的链接时遇到了问题(请参阅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参数中的第一个图像,并使用ijson从json网站中的item2参数中获取图像。

提供的错误是:

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<--

任何建议将不胜感激。

0 个答案:

没有答案