python使用xmlrpc自动发布文章到wordpress

均益采集文章,一般是用python,而网站程序是wordpress的,本来想直接将文章插入数据库的,但是wordpress的数据表有点麻烦。
所以均益使用python的 wordpress_xmlrpc模块,这个模块的使用方法请看 https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
这个模块是国外开发的,说明文档也是英文的,看起来吃力的朋友。可以看一下均益总结这个模块常用的方法。

安装:

带有自定义栏目字段的发布文章代码

#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))</pre>

带有特色图像缩略图的发布文章

#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

文 / 均益个人博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容