如何使用 client.call 函数从 wordpress 博客获取所有帖子?

How to get all the posts from a wordpress blog using client.call function?

我正在使用 python wordpress_xmlrpc 库来提取 wordpress 博客数据。我想获取我的 wordpress 博客的所有帖子。这是我的代码

client = Client(url, 'user', 'passw')
all_posts = client.call(GetPosts())

但这只返回最新的 10 个帖子。有什么办法可以得到所有的帖子吗?

根据 documentation,您可以传递一个参数来指示要检索的帖子数:

client.call(GetPosts({'number': 100}))

或者,如果您想获取所有帖子,请查看:https://python-wordpress-xmlrpc.readthedocs.org/en/latest/examples/posts.html#result-paging

我是这样做的:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import posts


client = Client('site/xmlrpc.php', 'user', 'pass')
data = []
offset = 0
increment = 20
while True:
        wp_posts = client.call(posts.GetPosts({'number': increment, 'offset': offset}))
        if len(wp_posts) == 0:
                break  # no more posts returned
        for post in wp_posts:
                print(post.title)
                data.append(post.title)
        offset = offset + increment