首页 > 解决方案 > 如何访问推文对象的扩展 URL?

问题描述

基于 URL 的 Twitter API:https ://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#urls

我想从推文的“实体”属性中的“url”属性中获取“expanded_url”属性。现在我有这个代码:

if post["entities"]["urls"]:
    url = post["entities"]["urls"]
        print(url)

它以这种格式打印一个数组:

[{'display_url': 'xxx', 'indices': [xx, xx], 'expanded_url': 'xxx', 'url': 'xxx'}]

我想得到'expanded_url',并尝试过url["expanded_url"],它只是抛出一个错误:

TypeError: list indices must be integers or slices, not str

有谁知道如何检索 url 属性的子属性?

标签: pythonarraysurltwittertwitterapi-python

解决方案


推荐阅读