通过 PyTumblr 只返回 20 个帖子 [英] Getting only 20 posts returned through PyTumblr
问题描述
我正在使用 PyTumblr 返回我所有的帖子,但它只返回 20.我发现post 函数的 kwarg,称为 limit,但当我指定 1000 时,它仍然返回 20.知道我做错了什么吗?
I am using PyTumblr to return all of my posts, but it is only returning 20. I found the kwarg for the posts function, called limit, but when I specified 1000 it still returned 20. Any idea what I'm doing wrong?
CLIENT = pt.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
all_posts = CLIENT.posts(BLOG_URL, limit=1000)
推荐答案
Tumblr 的 API 只允许指定最多 20 的限制.因此,您的 1000 限制将被忽略,而您会得到 20.您必须将分页与 offset
参数结合使用.
Tumblr’s API only allows to specify a limit up to 20. So your limit of 1000 is being ignored and you get 20 instead. You will have to use paging in combination with the offset
parameter instead.
您可以自己编写一些生成器,类似于无限滚动,只要您不断从中请求更多帖子,它就会请求下一页:
You could write yourself some generator which—similar to infinite scrolling—requests the next page as long as you keep requesting more posts from it:
def getAllPosts (client, blog):
offset = 0
while True:
posts = client.posts(blog, limit=20, offset=offset)
if not posts:
return
for post in posts:
yield post
offset += 20
这篇关于通过 PyTumblr 只返回 20 个帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!