官方网址: https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
github:https://github.com/maxcutler/python-wordpress-xmlrpc
1.安装:

easy_install python-wordpress-xmlrpc
pip install python-wordpress-xmlrpc

上传图片样例:

#coding:utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods import media
import filetype
wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
filename = './my.jpg' #上传的图片文件路径
data = {
        'name': 'my.jpg', # 文件名
}
with open(filename, 'rb') as f:
        img_content = xmlrpc_client.Binary(f.read())

# 获取文件的mimetype
kind = filetype.guess(img_content )
if not kind:
    return
data['type'] = kind.mime
data['bits'] = img_content 
response = wp.call(media.UploadFile(data))
#----------response常用的数据---------------------
{
    'url': 'http://aureua.com/wp-content/uploads/2019/02/iwr1mnQpngM-1.jpg',
    'type': 'image/jpeg',
    'link': 'http://aureua.com/wp-content/uploads/2019/02/iwr1mnQpngM-1.jpg',
    'description': '',
    'parent': 0,
    'title': 'iwr1mnQpngM.jpg',
    'thumbnail': 'http://aureua.com/wp-content/uploads/2019/02/iwr1mnQpngM-1-150x150.jpg',
    'file': 'iwr1mnQpngM.jpg',
    'attachment_id': '823',
    'id': '823'

文章发布样例:

#coding:utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
from datetime import datetime
 
wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
 
post = WordPressPost()
post.title = '文章标题'
post.content = '文章内容'
post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
post.terms_names = {
    'post_tag': ['python', 'xmlrpc'], #文章所属标签,没有则自动创建
    'category': ['python', 'xmlrpc'] #文章所属分类,没有则自动创建
 }
 
post.custom_fields = []   #自定义字段列表
post.custom_fields.append({  #添加一个自定义字段
        'key': 'price',
        'value': 5
})
post.custom_fields.append({ #添加第二个自定义字段
        'key': 'address',
        'value': '天涯海角'
})

post.date = datetime.strptime('2019-02-26', '%Y-%m-%d') # 发布时间
post.thumbnail = 11 #缩略图的id
post.id = wp.call(posts.NewPost(post))

文章分类和标签的创建

#coding:utf-8
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import taxonomies
 
wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
 
 
#新建标签
tag = WordPressTerm()
tag.taxonomy = 'post_tag'
tag.name = '标签名称'
tag.slug = '标签别名,可以忽略'
tag.id = wp.call(taxonomies.NewTerm(tag))  #返回的id
 
 
#新建分类
cat = WordPressTerm()
cat.taxonomy = 'category'
cat.name = '分类名称'
cat.slug = '分类别名,可以忽略'
cat.id = wp.call(taxonomies.NewTerm(cat)) #新建分类返回的id
 
 
 
#新建子分类
parent_cat = client.call(taxonomies.GetTerm('category', 20))#20是父分类的id
 
child_cat = WordPressTerm()
child_cat.taxonomy = 'category'
child_cat.parent = parent_cat.id
child_cat.name = '分类名称'#
child_cat.slug = '分类别名,可以忽略'#
child_cat.id = wp.call(taxonomies.NewTerm(child_cat))#新建分类返回的id
最后修改:2021 年 04 月 05 日
如果觉得我的文章对你有用,请随意赞赏