发纯文字的文章
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
import collections
collections.Iterable=collections.abc.Iterable
# 官方文档 https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
wp = Client('http://s.shutu.icu/xmlrpc.php', 'name', 'password')
wp.call(GetPosts())
wp.call(GetUserInfo())
post = WordPressPost()
post.title = 'My new title'
post.content = 'This is the body of my new post.'
post.post_status = "publish" # 状态 publish是发布,draft是草稿
post.terms_names = {
'post_tag': ['test', 'firstpost'], # 标签,无则新增
'category': ['Introductions', 'Tests']} # 分类
wp.call(NewPost(post))
发布图片
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)
发布多图
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.methods.media import UploadFile
# from wordpress_xmlrpc.compat import xmlrpc_client
import os
image_content_list=['图片1','图片2','图片3','图片4']
def create_gallery_post(images_folder, wordpress_url, wordpress_username, wordpress_password):
# 登录到 WordPress
client = Client(wordpress_url, wordpress_username, wordpress_password)
# 创建 WordPress 文章
post = WordPressPost()
post.title = "批量发布图片"
post.content = "终于解决批量上传图片的问题了"
post.excerpt = '解决!'
# 遍历图片文件夹
kk=0
for filename in os.listdir(images_folder):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
image_path = os.path.join(images_folder, filename)
# 上传图片到 WordPress
data = {
'name': filename,
'type': 'image/jpeg', # 或者其他图片格式
}
with open(image_path, 'rb') as img:
data['bits'] = img.read()
response = client.call(UploadFile(data))
# response = client.call(client.get_method('wp.uploadFile'), data, img)
attachment_id = response['id']
img_url = response['url']
print(attachment_id)
print(img_url)
print(type(attachment_id))#<class 'str'>
# 添加图片内容和图片url到文章内容中
post.content +=image_content_list[kk]+'\n'+f'<img src="{img_url}" alt="{filename}" />'
kk=kk+1
# 发布图库文章到 WordPress
post.post_status = 'publish'
client.call(NewPost(post))
print("图库文章发布成功!")
# 图片文件夹路径
images_folder = './jpg' # 替换为实际的图片文件夹路径
# WordPress 站点信息
wordpress_url = 'http://你的域名.后缀名/xmlrpc.php'
wordpress_username = '你的wordpress的账号名'
wordpress_password = '你的wordpress的账号密码'
# 创建图库文章(含多张图片的文章)并发布到 WordPress
create_gallery_post(images_folder, wordpress_url, wordpress_username, wordpress_password)
(Visited 27 times, 1 visits today)
本文作者为书途,转载请注明。