【问题标题】:Get media with Instagram Python API使用 Instagram Python API 获取媒体
【发布时间】:2015-12-10 08:14:58
【问题描述】:

我希望能够列出来自授权帐户的最近媒体项目的 URL。这是我第一次尝试使用 Python-Instagram API。我在下面的sn-p中填写了所有敏感变量。

from instagram.client import InstagramAPI
from urllib.request import urlopen
import json

access_token = ""
client_secret = ""
api = InstagramAPI(access_token=access_token, client_secret=client_secret)

user_id = ""

def get_media():
    request = 'https://api.instagram.com/v1/users/{USERID}/media/recent/?access_token={ACCESS_TOKEN}'
    response = urlopen(request).read().decode('utf8')
    obj = json.loads(response)

    for i in obj['data']['link']:
        print (i['link'])

这绝对不会返回任何内容。

print (obj) 以纯文本形式返回 json。 (这是正确的做事方式吗?或者 API 中是否有像 api.get_recent_media() 这样的调用?

【问题讨论】:

  • 是的,api客户端库中有调用。如果您只是手动发出请求,我不明白为什么要导入并实例化它。
  • API 提供了api.user_recent_media(user_id, count, max_id) 方法。可以看到方法和端点documented here的关系。
  • @CarlGroner 当我使用内置方法时,我得到一个KeyError: 'data'。想法?

标签: python json api instagram


【解决方案1】:

API 提供了一个user_recent_media 方法。这是docs 中给出的关于如何使用它的示例:

recent_media, next_ = api.user_recent_media()
photos = []
for media in recent_media:
    photos.append('<img src="%s"/>' % media.images['thumbnail'].url)

响应的格式可以从endpoint documentation 中收集。

【讨论】:

    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多