通过PyTumblr只返回20个帖子

Mat*_*son 2 python tumblr

我使用PyTumblr返回我的所有帖子,但它只返回20.我发现了post函数的kwarg,称为limit,但是当我指定1000时它仍然返回20.任何想法我做错了什么?

CLIENT = pt.TumblrRestClient(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
all_posts = CLIENT.posts(BLOG_URL, limit=1000)
Run Code Online (Sandbox Code Playgroud)

pok*_*oke 5

Tumblr的API仅允许指定最多20个限制.因此,您的1000限制将被忽略,而您将获得20个限制.您必须将分页与offset参数结合使用.

你可以自己写一些生成器 - 类似于无限滚动 - 请求下一页,只要你不断请求更多帖子:

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
Run Code Online (Sandbox Code Playgroud)