Instagram API python用户紧随其后

时间:2014-10-22 17:00:12

标签: python api instagram

我使用“api.user_followed_by”,它只显示50个用户,然后显示所有用户的链接,例如"https://api.instagram.com/v1/users/<id>/followed-by?access_token=<ACCESS-TOKEN>" 我需要在数据库中保存所有用户,有一种方法可以显示用户的所有关注者吗?也许是因为我在shell中做这个?

([User: victoriasecretmcb, User: laueleizalde, User: marellasdesign, User: caetano_nuno, User: eloy_fernandes, User: sparkles_design, User: xavinavas23, User: kikexr, User: ds_franklin, User: danielcaetano15, User: carlosalicova, User: donmiguelv, User: psteinvorth, User: hakvdub, User: _abrah, User: 7dimitri7, User: j.capote, User: roger_hannouch, User: gguerrero26, User: macmaquillaje02, User: ilgenuas, User: alem274, User: zebrols, User: edwinaleman69, User: gfmmartins, User: dego5, User: eliocastaneda3, User: jeancastilloc, User: dajosama, User: jamesdancer73, User: gavoonmaguire, User: nolygarcia62, User: maajiswim_siut, User: jjuankab, User: tikiticas, User: adefreitasvz, User: tenias66, User: obliviousowl_, User: lykus, User: mc_hasel, User: gabriela_m_oropeza_b, User: gabosanabria, User: adzamski, User: eishynena, User: tropical_chik_ve, User: teddy_bear567, User: cae_ucv, User: nikeexclusivo1, User: pablojmg, User: felixizarra], 'https://api.instagram.com/v1/users/307622607/followed-by?access_token=307622607.5593d6d.0936a78b99054931982b9b644ec75f48&cursor=1402704073642')

enter image description here

2 个答案:

答案 0 :(得分:1)

你需要使用分页。 API响应应包含一个名为next_url的值,调用您将获得50个以上的用户。如果没有分页,就无法一次性完成所有这些操作。

请参阅http://instagram.com/developer/endpoints/

在您的情况下,您似乎正在使用Python API库,因此在致电as_generator=True时添加user_followed_by。那应该返回一个你可以迭代的生成器对象。

def example():
    generator = api.user-followed_by(as_generator=True, max_pages=None)
    for page in generator:
        print page #page will have the next 50 users, etc

答案 1 :(得分:1)

在Instagram中处理分页是一种更简单的方法。此功能适用于您所关注的所有内容,您可以将其更改为您关注的所有内容。所有用户的值都将在current_following的集合中,这是一个从分页循环内部存储的类变量。

currently_following = set([])
def parse_following(next_url=None):
    if next_url == None:
        urlUserMedia = "https://api.instagram.com/v1/users/self/follows?access_token=%s" % (auth_token)
    else:
        urlUserMedia = next_url
    values = {
              'client_id' : client_id}
    try:
        data = urllib.urlencode(values)
        req = urllib2.Request(urlUserMedia,None,headers)
        response = urllib2.urlopen(req)
        result = response.read()
        dataObj = json.loads(result)
        next_url = None
        if dataObj.get('pagination') is not None:
            next_url = dataObj.get('pagination').get('next_url')
            currently_following.update(user['id'] for user in dataObj['data'])
        if next_url is not None:
            parse_following(next_url)

    except Exception as e:
        print e