pythonlistrssfeedparser

Extract value from a list formatted like a JSON file in Python


I have a function that returns an image URL from an RSS feed (I'm using feedparser). The problem is that it returns a strange formatted list.

My feed has a key called media_content. The code used to access it is

import feedparser
NewsFeed = feedparser.parse("https://thegroovecartel.com/feed/")
entry = NewsFeed.entries[0]
post_image = entry.media_content

Now, for some reason post_image is a list of 1 element formattes as:

[{'url': 'https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1', 'medium': 'image'}]

Now, I can't understand how to access the actual URL field to have https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1 as a string into a variable.


Solution

  • post_image = [{'url': 'https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1', 'medium': 'image'}]
    print(post_image[0]['url'])
    

    Result

    https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1
    

    You've got to treat post_image as a list, assessing its first element (i.e. element with index 0), gives you the dictionary that contains your image's attribute