wordpress XML转换为html

要实现将XML文档转换为HTML并提取特定内容,可以按照以下步骤编写Python程序:

import xml.etree.ElementTree as ET
import os
import re
from html import escape

def sanitize_filename(title):
    """处理非法文件名字符并限制长度"""
    filename = re.sub(r'[^\w\u4e00-\u9fff]', '_', title)
    filename = re.sub(r'_+', '_', filename).strip('_')
    return filename[:50] + '.html'

def process_xml(xml_path, output_dir):
    # 创建输出目录
    os.makedirs(output_dir, exist_ok=True)

    # 定义XML命名空间
    namespaces = {
        'wp': 'http://wordpress.org/export/1.2/',
        'content': 'http://purl.org/rss/1.0/modules/content/'
    }

    # 解析XML文件
    tree = ET.parse(xml_path)
    root = tree.getroot()

    # 遍历所有item节点
    for item in root.findall('.//item'):
        # 检查是否为文章类型
        post_type = item.find('wp:post_type', namespaces)
        if post_type is None or post_type.text != 'post':
            continue

        # 提取标题和内容
        title = item.find('title').text
        content = item.find('content:encoded', namespaces)

        if title is None or content is None:
            continue

        # 转义HTML特殊字符
        safe_title = escape(title)
        safe_content = content.text if content.text else ''

        # 构建HTML结构
        html = f'''<div class="post">
    <h1>{safe_title}</h1>
    {safe_content}
</div>'''

        # 生成文件名并保存
        filename = sanitize_filename(title)
        output_path = os.path.join(output_dir, filename)

        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(html)

if __name__ == '__main__':
    # 使用示例
    process_xml('WordPress.2025-03-25.xml', 'test')

程序说明:

  1. XML解析:使用ElementTree解析XML文档,处理WordPress的命名空间
  2. 内容过滤:只处理文章类型(post)的条目
  3. HTML生成
    • 自动转义特殊字符防止XSS
    • 包裹在

    容器中
    • 保留原始HTML格式内容
  4. 文件保存
    • 文件名基于标题自动生成
    • 自动处理非法字符和长度限制
    • 保存到指定输出目录

使用步骤:

  1. 将wordpres下载的xml文件保存WordPress.2025-03-25.xml
  2. 运行脚本,自动创建test目录
  3. 每个文章会生成单独的HTML文件,文件名格式为处理后的标题.html

注意事项:

  • 需要Python 3.6+环境
  • 保留原始HTML格式中的图片链接等资源
  • 自动处理中/英文字符和空格
  • 输出目录会自动创建,无需手动建立

该程序能够高效处理WordPress导出的XML文档,自动完成格式转换、内容提取和文件分割保存等操作。

评论

  1. 3 周前
    2026-8-27 19:46:31

    Really appreciate the customer support here. They helped me out quickly. Great vibes at phjili49!

  2. 2 周前
    2026-8-31 13:30:22

    I have tried a few platforms lately but this one really stands out. The interface is smooth and the payouts are surprisingly fast. Definitely worth a try for anyone looking for a reliable spot. Check out kr855win for a great experience.

  3. 2 周前
    2026-9-01 1:03:32

    Really impressed with the game selection and the overall vibe of the site. It feels like a community for real players. Everything is just right. Explore bdrana88.

  4. 2 周前
    2026-9-02 13:30:06

    I’m really impressed with the variety of games available here. Everything is so polished and the withdrawals are processed quickly. Go to okjiliapp and see for yourself.

  5. 2 周前
    2026-9-03 12:01:20

    The app version is just seamless. Everything is so organized and easy to navigate. I’m really enjoying my time on bd4999betapk.

  6. 1 周前
    2026-9-04 16:13:56

    I’ve been playing here for a few weeks now and the payout speed is actually impressive. Definitely one of my favorite spots to unwind. Check out phwin7 for a great experience!

  7. 1 周前
    2026-9-05 11:37:37

    I’ve tried a few sites lately but this one is definitely smoother. The login process is quick and I didn’t run into any lag while playing. Highly recommend oxibet88login for anyone looking for a stable platform.

  8. 1 周前
    2026-9-05 16:27:55

    I am really impressed with the bonus offers they have for new players. It is a great way to start winning at winpluscasino

  9. 1 周前
    2026-9-06 9:55:21

    If you are looking for a place with a huge range of betting options, this is it. Very transparent and easy to navigate. Visit miliarbetlogin.

  10. 5 天前
    2026-9-09 2:03:04

    The jackpot hits here are actually legit. I had a great run last night and the withdrawal was surprisingly fast. Highly recommend juragan88slot to all slot lovers!

  11. 3 天前
    2026-9-11 9:16:20

    The app is super stable and loads quickly. No lags or crashes which is exactly what I need when playing. Love the setup at x111gameloginapk.

  12. 3 天前
    2026-9-11 11:35:21

    This place has a great vibe and the bonuses are actually useful. It’s hard to find a site this reliable. Try phbar777

  13. 1 天前
    2026-9-13 7:23:31

    If you are looking for a seamless betting app experience this is it. Easy download and very intuitive design. Visit jeetbuzzappdownload.

发送评论 编辑评论


				
上一篇
下一篇