Python更新WordPress文章

最近想做一个服务器后台更新了SS配置知道然后自动更新到worpress网站的功能。
所以在网上找到了python-wordpress-xmlrpc。
→链接←
基本上只使用了一些基本功能:
总结一下,方便以后使用

获取服务器

第一条为wrodpress的网址,后面需要加/xmlrpc.php,然后填写用户名和密码

#需要导入的库
from wordpress_xmlrpc import Client

#获取服务器
client = Client("yoururl/xmlrpc.php","**username**","**password**")

新建文章

#需要导入的库
from wordpress_xmlrpc import WordPressPost

#新建文章
post = WordPressPost()

获取文章

第一条为wrodpress的网址,后面需要加/xmlrpc.php,然后填写用户名和密码

#需要导入的库
from wordpress_xmlrpc.methods import posts

#获取指定id的文章
post = client.call(posts.GetPost(171))

编辑文章内容

创建或者获取之前的文章之后就可以开始修改文章内容了。

#文章标题
post.title = '这是文章标题'
#文章正文
post.content = '这是文章正文~~'
#标签和分类
post.terms_names = {
        'post_tag': ['标签1'],        #可以添加多个标签,用逗号分隔。
        'category': ['分类名称'],
}
#是否置顶True置顶。False不置顶
post2.sticky= True

#文章状态publish未发布,默认为草稿
post.post_status = 'publish'

修改特色图片

特色图片就是在首页中再文章旁边显示出来的那个图片,如果不填的就是系统默认的图片。

#需要导入的库
from wordpress_xmlrpc.methods import posts,media

#上传图片
data = {
        'name': 'pictrue.jpg',
        'type': 'image/jpeg',  # mimetype
}
with open(filename, 'rb') as img:
        data['bits'] = xmlrpc_client.Binary(img.read())
response = client.call(media.UploadFile(data))

#修改默认图片
post.thumbnail = response['id']

上传修改文章

#修改文章指定id的文章,post为文章内容,用现在的post覆盖以前的post
client.call(posts.EditPost(171,post))

上传新文章

#修改文章指定id的文章,post为文章内容,用现在的post覆盖以前的post
client.call(posts.NewPost(post))

举个例子

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

#获取服务器
client = Client("http://******/xmlrpc.php","**username**","**password**")

#新建文章
post = WordPressPost()
post.title = '我是文章标题'
post.content = '我是正文~'
post.post_status = 'publish'
post.id = client.call(posts.NewPost(post))

#修改文章
post2 = client.call(posts.GetPost(post.id))
post2.terms_names = {
        'post_tag': ['标签1'],        #可以添加多个标签,用逗号分隔。
        'category': ['分类名称'],
}
post2.sticky= True
client.call(posts.EditPost(post.id,post))
转载地址:http://www.alaya.moe/255
2018年7月1日

0回复Python更新WordPress文章"

留言

邮箱地址不会被公开。 必填项已用*标注

2017-2021仙人掌教育版权所有
京ICP备18004382号-1