【问题标题】:How to get all the posts from a wordpress blog using client.call function?如何使用 client.call 函数从 wordpress 博客中获取所有帖子?
【发布时间】:2015-06-02 05:11:43
【问题描述】:

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

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

但这只会返回最新的 10 个帖子。有什么方法可以获取所有帖子吗?

【问题讨论】:

  • 没用过这个库,但是文档说可以传一个参数表示你想要的帖子数:python-wordpress-xmlrpc.readthedocs.org/en/latest/ref/…
  • 是的,我试过了。即使我传递了任何特定的数字,它也会返回正好 10 个帖子。
  • 你是如何传递number参数的?
  • client.call(GetPosts())
  • 文档说您必须将其作为dict 传递。我会把它写下来作为澄清的答案

标签: python wordpress xmlrpclib xmlrpcclient


【解决方案1】:

这就是我的做法:

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

【讨论】:

    【解决方案2】:

    根据documentation,你可以传递一个参数,表示你想检索多少个帖子:

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

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

    【讨论】:

    • 谢谢,还有一件事。有什么办法可以知道帖子总数。因为我事先不知道一个用户有多少帖子,我想获取特定用户的所有帖子。
    • 试试答案中提到的结果分页方式
    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多