栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/580512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号