在我的上一篇文章中已经给出了python操作wordpress的环境和发布文字的教程:
python直接发布到网站wordpress之一只发布文字-CSDN博客
本篇实现发布带图片的内容,无图无真相嘛。
直接上代码:
from wordpress_xmlrpc.methods.media import UploadFile
from wordpress_xmlrpc import Client
from wordpress_xmlrpc import WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
# Your credentials
url = 'http://你的域名.后缀名/xmlrpc.php'
username = '你的wordpress的账号名'
password = '你的wordpress的账号密码'
client = Client(url, username, password)
# Define your image and its properties
data = {
'name': 'big_image.jpg',
'type': 'image/jpeg', # mimetype
}
# Read the binary file and let the XMLRPC library encode it into base64
with open('big_image.jpg', 'rb') as img:
data['bits'] = img.read()
response = client.call(UploadFile(data))
attachment_id = response['id']
def send_post(title, content, attachment_id):
post = WordPressPost()
post.title = title
post.content = content
post.post_status = 'publish'
post.thumbnail = attachment_id
post_id = client.call(NewPost(post))
# Call the function
send_post("Your Title", "Your Content", attachment_id)
注:big_image.jpg是和代码.py同目录下的要发布的图片文件。