python-wordpress-xmlrpc发布更新修改文章功能介绍

木卢之

python-wordpress-xmlrpc发布更新修改文章功能介绍

前做的博客是wordpress程序的,最近又折腾了一个wordpress的博客,就是uziseo的网站,目前是用python在更新内容,今天就给大家整理以下关于自动更新worpress网站功能的很重要的一个第三方库——python-wordpress-xmlrpc模块包。下面请看它的相关内容:
获取服务器
第一条为wrodpress的网址,后面需要加/xmlrpc.php,然后填写用户名和密码
#需要导入的库
from wordpress_xmlrpc import Client
除了需要导入上面的库,我们还需要以下几个库,从而能够实现我们后面需要的获取、发布、修改wordpress文章的功能。
#python获取wordpress文章需要导入的库
from wordpress_xmlrpc.methods import posts

#python发布wordpress文章需要导入的库 
from wordpress_xmlrpc import WordPressPost

#python修改wordpress文章需要导入的库 
from wordpress_xmlrpc.methods import posts,media
#获取服务器
client = Client("yoururl/xmlrpc.php","**username**","**password**")
新建文章

#新建文章 post = WordPressPost()
#获取指定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'
修改特色图片
特色图片就是在首页中再文章旁边显示出来的那个图片,如果不填的就是系统默认的图片。
#上传图片
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))
举个例子
#新建文章
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))
留言与评论(共有 0 条评论)
   
验证码: