因为wordpress自带XMLRPC功能,所以,我们可以通过Python的XMLRPC接口进行wordpress的操作。

详情参考:http://python-wordpress-xmlrpc.readthedocs.io/en/latest/


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

安装:

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 import WordPressPage
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
import sys
import chardet
import MySQLdb
 
#build wordpress connection
wp = Client('http://localhost/cms-develop/wordpress/wordpress/xmlrpc.php', 'admin', 'admin');
post = WordPressPost()
 
try:
    #python mysql operation
    dbconn = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "root", db = "mmpic", charset = 'utf8')
    cursor = dbconn.cursor()
    cursor.execute("SELECT DISTINCT title FROM mmonly limit 0, 10")
    rows = cursor.fetchall()
    for row in rows:
        print "title = %s" % (unicode(row[0], 'utf-8'))
        #fencoding = chardet.detect(row[0])
        post.title = row[0]
        post.post_status = 'publish'
        post.terms_names = {}
        data = {}
        up_filename = ""
        post.content = ""
        attachment_id = 0
        #post.thumbnail = attachment_id
        cursor.execute("SELECT filename, description, category, foldername FROM mmonly WHERE title = %s ORDER BY description desc", row[0])
        child_rows = cursor.fetchall()
        current = 0
        count = cursor.rowcount
        for prow in child_rows:
            print "filename = %s, description = %s, category = %s, foldername = %s" % (prow[0], unicode(prow[1], 'utf-8'), unicode(prow[2], 'utf-8'), prow[3])
            current += 1;
            print "current is %d" % current
            nextpagestr = ""
            if (current == 1) :
                post.terms_names = {
                    'post_tag': [prow[2]],
                    'category': [prow[2]],
                }
                data = {
                    'name' : prow[0],
                    'type': 'image/jpeg',  # mimetype
                }
                up_filename = r'F:/aikanmeizi/' + prow[3] + "/" + prow[0]
                print "up_filename:" + up_filename
                with open(up_filename, 'rb') as img:
                    data['bits'] = xmlrpc_client.Binary(img.read())
                response = wp.call(media.UploadFile(data))
                attachment_id = response['id']
                post.thumbnail = attachment_id
            if (current == count):
                nextpagestr = ""
            else:
                nextpagestr = "<!--nextpage-->"
            post.content += prow[1] + "<img src=http://192.168.1.2/cms-develop/wordpress/wordpress/" + prow[3] + "/" + prow[0] + " /><br />" + nextpagestr
             
        post.id = wp.call(posts.NewPost(post))
    print "Number of rows returned: %d" % cursor.rowcount
 
    cursor.close()
    dbconn.close()
except MySQLdb.Error,e:
    print "Mysql Error %d: %s" % (e.args[0], e.args[1])

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


#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





点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部