从Python中格式类似于JSON文件的列表中提取值

2024-09-29 23:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从RSS提要返回图像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作为字符串放入变量中


Tags: httpsimagecomurl列表格式contentpost
1条回答
网友
1楼 · 发布于 2024-09-29 23:32:13
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

您必须将post_image视为一个列表,评估它的第一个元素(即索引为0的元素),为您提供包含图像属性的字典

相关问题 更多 >

    热门问题