Pythonic使用下一页令牌的方式

时间:2016-01-06 09:57:56

标签: python recursion while-loop python-requests instagram

很多请求现在返回页面令牌,我很好奇最合适的处理方式是什么?

我通常刚刚接受了递归:

def get_followers(user_id,
                  access_token,
                  cursor=''):
    url = 'https://api.instagram.com/v1/users/%s/followed-by?access_token=%s&cursor=%s' % (my_user,
                                                                                           access_token,
                                                                                           cursor)
    print(url)
    response = requests.get(url)
    json_out = json.loads(response.text)
    for user in json_out['data']:
        usrname = user['username']
        my_followers.add_follower(usrname)
        print("Added: %s" % usrname)
    try:
        cursor = json_out['pagination']['next_url']
        get_followers(user_id, access_token, cursor)
    except KeyError:
        print("Finished with %d followers" % len(my_followers.followers))
    return

然而,也许是:

while True:
  ..
  if condition:
    break

或者其他一些实现被视为更有效/ pythonic?

2 个答案:

答案 0 :(得分:3)

您可以将此更改为generator函数yield访问结构时的下一个关注者,仅在必要时获取更多数据。

def get_followers(...)
    ...
    while token is not None:
        # fetch data
        ...
        for user in json_out['data']:
            yield user
        ...
        # fetch new token

然后,遍历此生成器以应用您的数据处理。这也具有分离数据采集和处理逻辑的优点。

followers = get_followers(...) 
for user in followers:
        username = user['username']
        my_followers.add_follower(username)
        print("Added: %s" % username)

答案 1 :(得分:0)

感谢您的帮助,除了隐式(范围与xrange)之外,我从未使用过生成器。我很好奇,如果在这种情况下它提供任何速度/内存优势?

我的最终代码段:

def get_followers(user_id,
                  access_token,
                  cursor=''):
    """ Create a generator which will be accessed later to get the usernames of the followers"""
    while cursor is not None:
        # fetch data
        url = 'https://api.instagram.com/v1/users/%s/followed-by?access_token=%s&cursor=%s' % (my_user,
                                                                                               access_token,
                                                                                               cursor)
        print(url)
        response = requests.get(url)
        json_out = json.loads(response.text)
        for user in json_out['data']:
            yield user

        # fetch new token
        try:
            cursor = json_out['pagination']['next_url']
        except KeyError:
            print("Finished with %d followers" % len(my_followers.followers))
            cursor = None

followers_gen = get_followers(my_user, access_token)  # Get followers
for usr in followers_gen:
    usrname = usr['username']
    my_followers.add_follower(usrname)
    print("Added: %s" % usrname)

与不使用发电机相比:

def get_followers(user_id,
                  access_token,
                  cursor=''):
    """ Create a generator which will be accessed later to get the usernames of the followers"""
    while cursor is not None:
        # fetch data
        url = 'https://api.instagram.com/v1/users/%s/followed-by?access_token=%s&cursor=%s' % (my_user,
                                                                                               access_token,
                                                                                               cursor)
        print(url)
        response = requests.get(url)
        json_out = json.loads(response.text)
        for usr in json_out['data']:
            usrname = usr['username']
            my_followers.add_follower(usrname)
            print("Added: %s" % usrname)

        # fetch new token
        try:
            cursor = json_out['pagination']['next_url']
            get_followers(user_id, access_token, cursor)
        except KeyError:
            print("Finished with %d followers" % len(my_followers.followers))
            cursor = None

get_followers(my_user, access_token)  # Get followers