因为wordpress自带XMLRPC功能,所以,我们可以通过Python的XMLRPC接口进行wordpress的操作。
详情参考:http://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 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])
文章的脚注信息由WordPress的wp-posturl插件自动生成
微信扫一扫,打赏作者吧~![[整理]how to run flask with pyqt5](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/03/pyqt_flask.png&w=280&h=210&zc=1)
![[已解决]LINK : fatal error LNK1158: cannot run 'rc.exe' 错误的解决办法](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2021/02/Snipaste_2021-02-17_15-18-26-1024x505.png&w=280&h=210&zc=1)
![[已解决]Python扩展模块 error: Unable to find vcvarsall.bat](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/11/Snipaste_2020-11-19_10-01-38.png&w=280&h=210&zc=1)
![[整理]PyQt画圆,动态变色](http://www.jyguagua.com/wp-content/themes/begin/timthumb.php?src=http://www.jyguagua.com/wp-content/uploads/2020/08/drawCircle.gif&w=280&h=210&zc=1)