gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:00:17 27 4
gpt4 key购买 nike

我有一个从 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 作为变量的字符串。

最佳答案

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 的元素),为您提供包含图像属性的字典

关于python - 从列表中提取值,格式类似于 Python 中的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64178336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com