【问题标题】:Instagram API: 'User' object has no attribute 'counts'Instagram API:“用户”对象没有属性“计数”
【发布时间】:2016-02-25 06:44:20
【问题描述】:

我正在使用 Python 的 python-instagram 库来访问 Instagram API 的 users/user-id 端点:

根据文档,这是上述端点生成的理想响应:

{
    "data": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
        "bio": "This is my bio",
        "website": "http://snoopdogg.com",
        "counts": {
            "media": 1320,
            "follows": 420,
            "followed_by": 3410
        }
}

现在,我正在尝试从生成的响应中查找与counts 属性关联的数据:

from instagram.client import InstagramAPI
access_token = <my_access_token>

api = InstagramAPI(client_secret='xxxxxxxx', access_token = access_token[0])
usr = api.user_search('XXXXX')

print usr[0].counts

但是,我遇到了以下错误:

AttributeError: 'User' object has no attribute 'counts'

我的代码似乎有什么问题?

【问题讨论】:

    标签: python python-2.7 instagram python-2.x instagram-api


    【解决方案1】:

    user_search 方法返回一个信息有限的用户列表:
    'bio', 'full_name', 'id', 'profile_picture', 'username', 'website' p>

    您必须使用 is id 访问列表中的每个用户,然后您可以使用 counts 属性: first_user = api.user(usr[0].id) 然后first_user.counts

    【讨论】:

    • 打印 usr[0]['counts'] 引发错误。这是 dir(usr[0]) 返回的内容:['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'bio', 'full_name', 'id', 'object_from_dictionary', 'profile_picture', 'username', 'website']。打印 usr[0] 返回:User: XXXX
    • 好的,搜索似乎返回了一个包含以下信息的用户列表:'bio'、'full_name'、'id'、'profile_picture'、'username'、'website' 。所以你不能使用 counts 属性。您必须使用他的 id 获取用户信息:ex_user = api.user(usr[0].id) 然后 ex_user.counts
    猜你喜欢
    • 2021-01-28
    • 2021-06-29
    • 2013-05-13
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多