Python 利用 xmlrpc-API 自动发布文章到 WordPress

笔记:Python 利用 xmlrpc-API 自动发布文章到 WordPress

Python 通过 xmlrpc 发布文章到 WordPress 的教程有很多,有详细也有简洁。但是,找到的教程或是通过方法博客提供的脚本也没有解决个人想要的。经过一番谷歌以及摸索折腾,终于搞定了支持定义标签别名和文章别名的发布脚本,做一个记录分享。

首先,提供一篇参考教程:Python 使用 xmlrpc 自动发布文章到 WordPress 以及相关的模块说明:Python-WordPress-xmlrpc 说明文档

其次,需要安装相关 Python 模块

  • easy_install python-wordpress-xmlrpc
  • # 或者
  • pip install python-wordpress-xmlrpc

然后,定义自己需要的发布文章脚本。

支持自定义栏目字段的发布文章代码

  • #coding:utf-8
  • 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 = 'publish' #文章状态,不写默认是草稿,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))

支持特色图像缩略图的发布文章代码

  • #coding:utf-8
  • 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 = 'publish' #文章状态,不写默认是草稿,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))
展开

创建新的分类和标签代码

  • #coding:utf-8
  • 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

支持自定义标签别名及文章别名代码

  • from wordpress_xmlrpc import Client, WordPressPost, WordPressTerm
  • from wordpress_xmlrpc.methods import taxonomies
  • from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
  • from wordpress_xmlrpc.methods.users import GetUserInfo
  • import sys
  • reload(sys)
  • sys.setdefaultencoding('utf-8')
  • try:
  • wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
  • #新建标签
  • tag = WordPressTerm()
  • tag.taxonomy = 'post_tag'
  • tag.name = 'Python教程'#标签名称
  • tag.slug = 'python-jiaocheng'#标签别名,可以忽略
  • tag.description = '' #标签图像描述,可以忽略
  • tag.id = wp.call(taxonomies.NewTerm(tag))#返回的id
  • print '标签添加成功!'
  • except:
  • print '标签添加失败!'
  • pass
  • try:
  • wp = Client('http://您的域名/xmlrpc.php', '后台账号', '后台密码')
  • post = WordPressPost()
  • post.title = 'Python 利用 xmlrpc-API 自动发布文章到 WordPress' #文章标题
  • post.content = 'Python 利用 xmlrpc-API 自动发布文章到 WordPress' #文章内容
  • post.slug = 'python-xmlrpc-wordpress' #文章别名,固定链接形式为文章标题时需要
  • post.post_status = 'publish'
  • post.terms_names = {
  • 'post_tag': ['Python教程'], #文章标签
  • 'category': ['网络技术'] #文章分类
  • }
  • wp.call(NewPost(post))
  • print '文章发布成功!'
  • except:
  • print '文章发布失败!'
  • pass
展开

 

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
<<上一篇
下一篇>>
目 录