简体   繁体   中英

Retrieving url from feedparser media:content

The following code works fine:

if 'media_content' in entry:
                mediaContent=entry.media_content
            else:
                mediaContent='No Media'

But this gives me an error:

 if 'media_content' in entry:
                mediaContent=entry.media_content['url']
            else:
                mediaContent='No Media'

This is the error:

list indices must be integers, not str

I've seen multiple uses of this syntax when searching on the web to pull out the url from the media_content element of feedparser, but it isn't working for me. I'm new to python, so I'm sure it is just that I'm not accessing the dictionary properly. I just want the raw url as a string.

Thanks!

entry.media_content is a list of entries, each a dictionary:

for item in entry.media_content:
    print item['url']

You could just grab the first one listed:

if 'media_content' in entry:
    mediaContent = entry.media_content[0]['url']

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