天天看點

python使用xmlrpc自動釋出文章到wordpress的好例子python使用xmlrpc自動釋出文章到wordpress的好例子

python使用xmlrpc自動釋出文章到wordpress的好例子

使用python的 wordpress_xmlrpc子產品,這個子產品的使用方法請看 https://python-wordpress-xmlrpc.readthedocs.io/en/latest/

這個子產品是國外開發的,說明文檔也是英文的,看起來吃力的朋友。可以看一下我總結這個子產品常用的方法。

安裝:

pip3 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
           

繼續閱讀