当前位置: 首页 > news >正文

python直接发布到网站wordpress之二发布图片

在我的上一篇文章中已经给出了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 = titlepost.content = contentpost.post_status = 'publish'post.thumbnail = attachment_idpost_id = client.call(NewPost(post))# Call the function
send_post("Your Title", "Your Content", attachment_id)

注:big_image.jpg是和代码.py同目录下的要发布的图片文件。 

 

http://www.lryc.cn/news/343717.html

相关文章:

  • Messari 报告摘要 :Covalent Network(CQT)2024 年第一季度表现
  • PGP加密技术:保护信息安全的利器
  • 【C++】文件
  • uniapp离线在Xcode上打包后提交审核时提示NSUserTrackingUsageDescription的解决方法
  • 【Linux】进程exec函数族以及守护进程
  • 为什么 ChatGPT 不火了?
  • Ubuntu22.04下安装kafka_2.11-0.10.1.0并运行简单实例
  • 【S32K3 MCAL配置】-7.2-GPT Driver:仿OS,周期/定时调用APP SWC和BSW模块的主函数
  • golang内置包里面的sort.Slice 切片排序函数使用示例
  • Golang | Leetcode Golang题解之第70题爬楼梯
  • 区块链 | NFT 相关论文:Preventing Content Cloning in NFT Collections(三)
  • Unity技术学习:渲染大量物体的解决方案,外加RenderMesh、RenderMeshInstanced、RenderMeshIndirect的简单使用
  • [数据概念|方案实操][最新]数据资产入表4月速递
  • C++中使用Multimap和Vector管理和展示数据
  • Java---类和方法的再学习
  • C语言每日一练(12、水仙花数)
  • HTML5实现酷炫个人产品推广、工具推广、信息推广、个人主页、个人介绍、酷炫官网、门户网站模板源码
  • 系统如何做好安全加固?
  • 对NI系统和PLC系统的应用比较
  • 微服务架构中的挑战及应对方式:Outbox 模式
  • 使用Docker安装MySQL5.7.36
  • 【PyTorch】6-可视化(网络结构可视化、CNN可视化、TensorBoard、wandb)
  • C++容器——map和pair对组
  • MVC和DDD的贫血和充血模型对比
  • 如何利用AI提高内容生产效率?
  • C++ stack、queue以及deque
  • 科沃斯,「扫地茅」荣光恐难再现
  • 双向BFS算法学习
  • C++从入门到精通---模版
  • Unity数据持久化之Json