这两天搞的,python使用xmlrpc对wordpress增加文章,为了配合使用python用邮件向wordpress增加文章的实验,现在都做得差不多了,记录下代码!
#coding=gbk
import xmlrpclib
def send_data_to_wp(wp_url,wp_username,wp_password,title,content,tags,fenlei):
wp_blogid=”
status_published = 0
server = xmlrpclib.ServerProxy(wp_url)
aa=fenlei
#中文的gbk转换成utf8格式, 不转换是会出错
aa=aa.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
categories = [aa]
#中文的gbk转换成utf8格式
title=title.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
#中文的gbk转换成utf8格式
content=content.decode(‘gbk’,’ignore’).encode(‘utf8′,’ignore’)
data = {‘title’: title, ‘description’: content, ‘categories’: categories, ‘mt_keywords’: tags,’post_status’:’publish’}
print data
post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)
return post_id
# 需要操作的wordpress
w_url = “http://192.168.5.25/wordpress/xmlrpc.php”
#登录用户
w_username = “admin”
#登录密码
w_password = “123456789”
#文章标签
w_tags = [“sometag”, “othertag”]
#文章题目
w_title=’中文测试’
#文章内容
w_content=’今天天气不错啊’
#文章分类
w_fenlei=’一般想到的’
print send_data_to_wp(w_url,w_username,w_password,w_title,w_content,w_tags,w_fenlei)
另外关于wordpress 的xmlrpc使用方法网址:http://codex.wordpress.org/XML-RPC_wp , 比较有用!
以下这段代码是列出xmlrpc的使用方法有哪些
mport xmlrpclib
from pprint import pprint
server = xmlrpclib.ServerProxy(“http://localhost/wordpress/xmlrpc.php”)
pprint(server.system.listMethods() )
这样应该比较方便应用了!