【问题标题】:When I request from instagram user info it's return only user name当我从 instagram 用户信息请求时,它只返回用户名
【发布时间】:2015-09-30 06:42:37
【问题描述】:

我用这个pythonlibrary

from instagram.client import InstagramAPI
access_token = "Access Token"
api = InstagramAPI(access_token=access_token)
user_info = api.user(user_id)
print user_info

我无法收到这个答案:

{
    "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
        }
}

我怎样才能检索生物、追随者计数等信息?谢谢你

【问题讨论】:

  • 那么,您到底收到了什么?尽可能使用 HTTP 状态码。

标签: python instagram instagram-api


【解决方案1】:

您实际上收到了整个响应,但 Instagram API 客户端的 Python 版本正在返回其 User 类的实例(继承自 ApiModel 类)。

您可以通过单独访问这些属性来获取所需的所有数据:

user = api.user('1574083')
print user.username
print user.full_name
print user.profile_picture
print user.bio
print user.website
print user.counts 

哪些打印:

snoopdogg
snoopdogg
http://scontent.cdninstagram.com/hphotos-xap1/t51.2885-19/11186934_976841435684008_1692889037_a.jpg
Get BUSH now!
http://smarturl.it/BushAlbum
{u'media': 19019, u'followed_by': 6746636, u'follows': 1707}

这是具有欺骗性的,因为如果您只打印user,您只会看到User: snoopdogg。在 Python 中,它可以覆盖许多其他语言中不允许的方法。特别是在这里他们已经覆盖了__str____repr____unicode__,它们都用于将对象转换为字符串类型的对象以进行打印。

我的理解是__str____unicode__ 应该是“漂亮”的版本,而__repr__ 应该向您展示(如果可能的话)如何重建对象。他们在实现这两个目标方面做得很差,但可以说它比对象的默认表示更好,比如<instagram.models.User object at 0xf8c5a750>

【讨论】:

  • 如何查看可访问的属性列表?
  • 在 Python 中有一个内置函数 dir,它返回参数 (docs.python.org/2/library/functions.html#dir) 的有效属性列表。对于这种情况,如果我运行print dir(user),我会得到一个列表,其中包括字段“bio”、“counts”、“full_name”、“id”、“object_from_dictionary”、“profile_picture”、“username”、“website”(以及一堆内置函数,如__str____dict__)。
猜你喜欢
  • 1970-01-01
  • 2018-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多