Python实现批量导入Markdown文件到WordPress(博客搬家工具,代码已开源)

本文同步发布于我的个人网站:https://jwblog.xyz

代码已开源:https://github.com/nefu-ljw/python-markdown-to-wordpress

1. 熟悉安装和使用python-wordpress-xmlrpc

python-wordpress-xmlrpc,这个其实就是别人写好的包,直接调用了 WordPress 的 xmlrpc 接口发布文章。

首先安装这个包,对于python3,用 pip3 install python-wordpress-xmlrpc 进行安装。

然后写个简单的程序,测试WordPress发文章:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost

post = WordPressPost()  # 初始化post,我们要发表的文章就是post

# post的一些属性
post.title = "Test: This is the title"  # 标题
post.content = "Test: This is the content"  # 内容
post.post_status = 'publish'  # 类型(publish发布、draft草稿、private隐私)
post.terms_names = {
    'post_tag': ['test-tag1', 'test-tag2'],  # 标签(可以写多个)
    'category': ['test-category']  # 分类(可以写多个)
}  # 如果标签、分类没有的话会自动创建,有的话也不影响
post.comment_status = 'open'  # 开启评论

# 客户端
client = Client('https://jwblog.xyz/xmlrpc.php', '账号', '密码')  # 改成自己的账号密码,jwblog.xyz改成你自己的域名
client.call(NewPost(post))

这个包的使用文档:https://python-wordpress-xmlrpc.readthedocs.io/en/latest/overview.html

2.编写自己的代码实现

所使用的 python 模块:

  • python-frontmatter:通过python-frontmatter库获取文章信息,标题、分类、标签、正文内容等
  • markdown:通过markdown库将正文内容转换成HTML格式
  • python-xmlrpc-wordpress:最后将这些信息通过python-wordpress-xmlrpc库发布到网站上

我的代码放在github上,安装和使用方法见README.md:
https://github.com/nefu-ljw/python-markdown-to-wordpress/blob/main/README.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nefu-ljw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值