簡體   English   中英

從格式類似於 Python 中的 JSON 文件的列表中提取值

[英]Extract value from a list formatted like a JSON file in 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作為字符串轉化為變量。

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 的元素),為您提供包含圖像屬性的字典

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM