如何用python使用xmlrpc自动发布文章到wordpress(可以发自定义栏目字段)记录

我们可以使用python的 wordpress_xmlrpc模块,这个模块的使用方法请看
https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
这个模块是国外开发的,说明文档也是英文的,看起来吃力的朋友,可以用翻译软件看看。以下是我在网上找到

首先我们要安装一下python-wordpress-xmlrp包:
pip3 install python-wordpress-xmlrpc 或者 pip install python-wordpress-xmlrpc
总结方法:

一、wordpress xmlrpc模块用python发布带有自定义栏目字段的发布文章代码

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

二、wordpress xmlrpc模块用python发布带有特色图像缩略图的发布文章

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
wp = Client(‘http://您的域名/xmlrpc.php’, ‘后台账号’, ‘后台密码’)
filename = ‘./my.jpg’ #上传的图片文件路径

prepare metadata

data = {
‘name’: ‘picture.jpg’,
‘type’: ‘image/jpeg’, # mimetype
}

read the binary file and let the XMLRPC library encode it into base64

with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
# response == {
#       'id': 6,
#       'file': 'picture.jpg'
#       'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
#       'type': 'image/jpeg',
# }
attachment_id = response['id']
post = WordPressPost()
post.title = '文章标题'
post.content = '文章正文'
post.post_status = 'draft'  #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
post.terms_names = {
	'post_tag': ['test', 'firstpost'], #文章所属标签,没有则自动创建
	'category': ['Introductions', 'Tests'] #文章所属分类,没有则自动创建
}
post.thumbnail = attachment_id #缩略图的id
post.id = wp.call(posts.NewPost(post))

二、wordpress xmlrpc模块用python单独创建新的分类和标签

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import taxonomies
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
#新建标签
tag = WordPressTerm()
tag.taxonomy = 'post_tag'
tag.name = 'My New Tag12'#标签名称
tag.slug = 'bieming12'#标签别名,可以忽略
tag.id = wp.call(taxonomies.NewTerm(tag))#返回的id
#新建分类
cat = WordPressTerm()
cat.taxonomy = 'category'
cat.name = 'cat1'#分类名称
cat.slug = 'bieming2'#分类别名,可以忽略
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 = 'My Child Category'#分类名称
child_cat.slug = 'beidongdui'#分类别名,可以忽略
child_cat.id = wp.call(taxonomies.NewTerm(child_cat))#新建分类返回的id

四、总结

基本上学会以上用法即可用python 操作在wordpress 网站上发布文章了,结合爬虫+gpt 可以想形成批量自动化生成高质量文章
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

longm龙哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值