博客 / WordPress/ 使用 Python wordpress_xmlrpc 模块批量发布文章到 WordPress

使用 Python wordpress_xmlrpc 模块批量发布文章到 WordPress

使用 Python wordpress_xmlrpc 模块批量发布文章到 WordPress

概述

Python 的 python-wordpress-xmlrpc 库提供了通过 XML-RPC 协议与 WordPress 站点交互的能力,可以实现自动化发布文章、管理分类标签、上传媒体文件等操作。本文介绍其基本安装、使用方法及常见场景的代码示例。

安装

使用 pip 安装:

pip install python-wordpress-xmlrpc

基础使用:发布文章

首先导入必要的模块并建立客户端连接。

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

# 建立连接
wp = Client('https://your-site.com/xmlrpc.php', 'username', 'password')

注意:请将 URL、用户名和密码替换为你自己的 WordPress 站点信息。建议使用应用密码而非管理员账户密码。

发布基础文章

post = WordPressPost()
post.title = '文章标题'
post.content = '文章内容'
post.post_status = 'publish'  # 文章状态:'draft'(草稿), 'publish'(发布), 'private'(私密)

# 设置标签和分类
post.terms_names = {
    'post_tag': ['标签1', '标签2'],
    'category': ['分类1', '分类2']
}

# 发布文章并获取文章ID
post_id = wp.call(posts.NewPost(post))
print(f"文章已发布,ID: {post_id}")

发布带有自定义字段的文章

自定义字段(Custom Fields)可用于存储文章的额外元数据。

post = WordPressPost()
post.title = '带自定义字段的文章'
post.content = '文章正文'
post.post_status = 'publish'

# 添加自定义字段
post.custom_fields = []
post.custom_fields.append({
    'key': 'price',
    'value': 99.9
})
post.custom_fields.append({
    'key': 'location',
    'value': '北京'
})

post_id = wp.call(posts.NewPost(post))

发布带有特色图片的文章

需要先上传图片作为媒体文件,获取其附件 ID,再将其设置为文章的缩略图。

from wordpress_xmlrpc.methods import media
from wordpress_xmlrpc.compat import xmlrpc_client

# 上传图片
filename = './featured-image.jpg'

with open(filename, 'rb') as img:
    data = {
        'name': 'featured-image.jpg',
        'type': 'image/jpeg',  # 根据实际文件类型修改
        'bits': xmlrpc_client.Binary(img.read())
    }

response = wp.call(media.UploadFile(data))
attachment_id = response['id']  # 获取上传图片的ID

# 创建文章并设置特色图片
post = WordPressPost()
post.title = '带特色图片的文章'
post.content = '文章正文'
post.post_status = 'publish'
post.thumbnail = attachment_id  # 设置特色图片ID

post_id = wp.call(posts.NewPost(post))

管理分类与标签

除了在发布文章时自动创建,也可以单独创建分类和标签。

创建新标签

from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import taxonomies

tag = WordPressTerm()
tag.taxonomy = 'post_tag'
tag.name = '新标签'
tag.slug = 'new-tag-slug'  # 可选,用于URL的别名

tag_id = wp.call(taxonomies.NewTerm(tag))
print(f"标签创建成功,ID: {tag_id}")

创建新分类

cat = WordPressTerm()
cat.taxonomy = 'category'
cat.name = '新分类'
cat.slug = 'new-category-slug'

cat_id = wp.call(taxonomies.NewTerm(cat))
print(f"分类创建成功,ID: {cat_id}")

创建子分类

# 假设父分类ID为 10
parent_cat_id = 10

child_cat = WordPressTerm()
child_cat.taxonomy = 'category'
child_cat.parent = parent_cat_id
child_cat.name = '子分类'
child_cat.slug = 'child-category'

child_cat_id = wp.call(taxonomies.NewTerm(child_cat))
print(f"子分类创建成功,ID: {child_cat_id}")

注意事项与最佳实践

  • 安全性:不要在代码中硬编码密码。考虑使用环境变量或配置文件来管理敏感信息。
  • 错误处理:在实际脚本中应添加 try-except 块来处理网络错误或认证失败等异常。
  • 批量操作:如需批量发布,建议在循环中添加适当的延时,避免对服务器造成过大压力。
  • 库版本:本文示例基于 python-wordpress-xmlrpc 的较新版本。请确保你安装的是最新版本。

通过上述方法,你可以灵活地使用 Python 脚本自动化管理你的 WordPress 内容。

发表评论

您的邮箱不会公开。必填项已用 * 标注。