如何打破递归函数

时间:2013-11-01 03:13:34

标签: python list mongodb recursion instagram

我正在使用Instagram API,这是我的代码;

from instagram.client import InstagramAPI
api = InstagramAPI(access_token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
tagname = 'naturelover'

photo_urls = []

def get_urls( skeyword, next_max_id=None ):

    tags_recent_media, next = api.tag_recent_media( max_id=next_max_id, tag_name = "%s"%skeyword )
    if tags_recent_media:
        [photo_urls.append(media.images['standard_resolution'].url) for media in tags_recent_media]
        if next is None:
            pass
        else:
            if next.find('max_id') == -1:
                next_max_id = next.split('max_tag_id=')[1]
                next_page_id="%s"%next_max_id.split('&')[0]
            else:
                next_max_id = next.split('max_id=')[1]
                if next_max_id.split('&'):
                    next_page_id="%s"%next_max_id.split('&')[0]

            get_urls( skeyword, next_page_id )
    else:
        pass
    return photo_urls

tag_media_data = get_urls(tagname,inc)
mongo.insert({'media':tag_media_data})

在每一个请求上,Instagram返回2o个最大图像,下一个网址用于分页,我试图存储基于标记的所有图像,我必须递归调用函数,直到下一个url 不是没有,接下来是一个网址,我将其切片以获取下一个网址ID(instagram api支持下一个ID用于分页)。但是我得到了一个最大递归调用错误,搜索这个错误我得到了一些答案,比如stackrecursion的设置值,但这在逻辑方面没有意义。我没有找到任何关于如何在一些递归调用或任何其他替代方式后中断递归的解决方案。

在代码的最后,我将所有列表数据保存在一个mongo文档中,换句话说,我在每次递归调用后保存数据,这样我在每个文档中都得到了冗余数据,如下所示:

photo_urls = []
[photo_urls.append(media.images['standard_resolution'].url) for media in tags_recent_media]
mongo.insert({'media':photo_urls})

我无法找到其他方式,我试图以其他方式做到这一点,请指出这个问题是否需要更具描述性。

1 个答案:

答案 0 :(得分:1)

递归总是可以表示为显式循环。考虑这种方法:

next_page_id = None
while True:
  tags_recent_media, next = api.tag_recent_media(keyword, next_page_id)
  extractImagesFrom(tags_recent_media)
  if not next:
    break
  next_page_id = ...

我敢打赌这是API作者的代码。