Getting only 20 posts returned through PyTumblr

蓝咒 提交于 2019-12-08 00:29:17

问题


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)

回答1:


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


来源:https://stackoverflow.com/questions/21689852/getting-only-20-posts-returned-through-pytumblr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!