我有一个带有一些JSON的API的URL:
Daniel Sanchez
其中{
"posts": [ ... ],
"page": { ... },
"next": "/posts.json?page=2"
}
的页码不同,如果没有其他页面,则可能为/posts.json?page=2
。
我如何在Python中创建一个输出包含所有帖子的所有页面的函数?
我想我必须做类似
的事情null
但我想我可以用def get_posts(url, posts=[]):
json = request(url).json()
posts.append(json.posts)
while json.next_page:
return get_posts(json.next_page, posts)
做点什么?
答案 0 :(得分:1)
def get_posts(url, posts=None):
# initialize the posts lists
posts = [] if posts is None else posts
# make the request and convert to json
json = request(url).json()
# extends the posts array with the returned posts
posts.extend(json['posts'])
# if there is a next_page, call the function recursively
if json.next_page:
return get_posts(json.next_page, posts)
# if there isn't a next_page, return the posts
return posts