? 编辑中: Post:21.body 保存 删除 取消
初始化中.....

最新主题

在Newsfeed关注关注中
+ 新主题
载入中...

标题

正文
^1 ^2 已添加 ━ 发布者 user_name
更多主题

Main -=ZeroNet开发讨论板块=- 如何把WordPress文章导入ZeroBlog?-自动化工具

在Newsfeed关注关注中

如何把WordPress文章导入ZeroBlog?-自动化工具

tags:

  • 自动化工具
  • WordPress
  • Python

系统依赖:

Windows Linux Mac Android iOS
通过 未测试 未测试 未测试 未测试

运行环境:Python3.6

环境包依赖:html2text

# -*- coding: UTF-8 -*-
import xml.dom.minidom
import time
import json
import html2text
from os.path import isfile

# -------Edit Below-------
data_json_path = ""
# Example: data_json_path = r"N:\ZeroBundle\ZeroNet\data\1NtyUa8gvLkwJeHYebVkZXA9hespXvTRFv\data\data.json"
WordPress_ExportXML_Path = ""
# Your WordPress Export XML File Path
# -------Edit End-------

if not isfile(data_json_path) or not isfile(WordPress_ExportXML_Path):
    print('Please Edit Me And Set data_json_path and WordPress_ExportXML_Path')
    exit(1)

h = html2text.HTML2Text()
site_json = open(data_json_path, encoding='utf-8').read()
open('site_data_json.bak', 'wb').write(site_json.encode())
site_json = json.loads(site_json)
current_post_id = site_json['next_post_id']
posts = site_json['post']

DOMTree = xml.dom.minidom.parse(WordPress_ExportXML_Path)
items = DOMTree.documentElement.getElementsByTagName("item")
for item in items:
    if not item.getElementsByTagName('content:encoded')[0].childNodes:
        continue
    post_title = item.getElementsByTagName('title')[0].childNodes[0].data
    post_time = items[1].getElementsByTagName('wp:post_date')[0].childNodes[0].data
    post_time = time.mktime(time.strptime(post_time, '%Y-%m-%d %H:%M:%S'))
    post_body = h.handle(item.getElementsByTagName('content:encoded')[0].childNodes[0].data)
    post = {"post_id": current_post_id, "title": post_title, "date_published": post_time,
            "body": post_body}
    posts.insert(0, post)
    current_post_id += 1

site_json['next_post_id'] = current_post_id
open(data_json_path, 'wb').write(json.dumps(site_json, ensure_ascii=False, indent="\t").encode())
print('Good! Convert Finished, Please sign site and published, then refresh the page, you will see your WordPress post')
^8 ^9 12hlearn 发表了 在 Jan 24, 2017
点击这里登录新评论
登录
提交评论 提问前请仔细看【置顶帖
您的限额将要用完,请到站务版块发帖要求增加限额,站长 @p2p 或者超级版主看到会处理,谢谢!
user_name已添加 ^1 ^2
回复
正文
birdvd在 Feb 21, 2018 ^1 ^2
回复

666

erobaby在 Oct 04, 2017 ^1 ^2
回复

Good

mach在 Sep 05, 2017 ^1 ^2
回复

cool

nangel在 Sep 05, 2017 ^1 ^2
回复

这个好

zerodo在 Aug 19, 2017 ^1 ^2
回复

new bee

This page is a snapshot of ZeroNet. Start your own ZeroNet for complete experience. Learn More