如何解决从Python格式类似于JSON文件的列表中提取值
我有一个函数可以从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的元素),为您提供包含图像属性的字典
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。