问题描述
我有一个函数可以从RSS Feed中返回图像URL(我正在使用Feedparser)。问题在于它返回了一个奇怪的格式化列表。
我的供稿有一个名为media_content
的密钥。用于访问它的代码是
import Feedparser
NewsFeed = Feedparser.parse("https://thegroovecartel.com/Feed/")
entry = NewsFeed.entries[0]
post_image = entry.media_content
现在,由于某种原因,post_image是1个元素的格式列表,格式为:
[{'url': 'https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1','medium': 'image'}]
现在,我不明白如何访问实际的URL字段以将https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1
作为字符串添加到变量中。
解决方法
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'])
结果
https://thegroovecartel.com/i1.wp.com/thegroovecartel.com/wp-content/uploads/2020/10/KAYYA.jpg?fit=300%2C237&ssl=1
您必须将post_image
视为一个列表,评估其第一个元素(即索引为0的元素),为您提供包含图像属性的字典