{"id":615,"date":"2025-04-08T01:20:18","date_gmt":"2025-04-07T17:20:18","guid":{"rendered":"https:\/\/www.gladguang.cn\/?p=615"},"modified":"2025-04-08T01:25:54","modified_gmt":"2025-04-07T17:25:54","slug":"ff60997340b544272daad048921e0773","status":"publish","type":"post","link":"https:\/\/www.gladguang.cn\/index.php\/2025\/04\/08\/ff60997340b544272daad048921e0773\/","title":{"rendered":"wordpress XML\u8f6c\u6362\u4e3ahtml"},"content":{"rendered":"<p>\u8981\u5b9e\u73b0\u5c06XML\u6587\u6863\u8f6c\u6362\u4e3aHTML\u5e76\u63d0\u53d6\u7279\u5b9a\u5185\u5bb9\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u7f16\u5199Python\u7a0b\u5e8f\uff1a<\/p>\n<div data-mode=\"python\">\n<pre><code>import xml.etree.ElementTree as ET\nimport os\nimport re\nfrom html import escape\n\ndef sanitize_filename(title):\n    \"\"\"\u5904\u7406\u975e\u6cd5\u6587\u4ef6\u540d\u5b57\u7b26\u5e76\u9650\u5236\u957f\u5ea6\"\"\"\n    filename = re.sub(r'[^\\w\\u4e00-\\u9fff]', '_', title)\n    filename = re.sub(r'_+', '_', filename).strip('_')\n    return filename[:50] + '.html'\n\ndef process_xml(xml_path, output_dir):\n    # \u521b\u5efa\u8f93\u51fa\u76ee\u5f55\n    os.makedirs(output_dir, exist_ok=True)\n\n    # \u5b9a\u4e49XML\u547d\u540d\u7a7a\u95f4\n    namespaces = {\n        'wp': 'http:\/\/wordpress.org\/export\/1.2\/',\n        'content': 'http:\/\/purl.org\/rss\/1.0\/modules\/content\/'\n    }\n\n    # \u89e3\u6790XML\u6587\u4ef6\n    tree = ET.parse(xml_path)\n    root = tree.getroot()\n\n    # \u904d\u5386\u6240\u6709item\u8282\u70b9\n    for item in root.findall('.\/\/item'):\n        # \u68c0\u67e5\u662f\u5426\u4e3a\u6587\u7ae0\u7c7b\u578b\n        post_type = item.find('wp:post_type', namespaces)\n        if post_type is None or post_type.text != 'post':\n            continue\n\n        # \u63d0\u53d6\u6807\u9898\u548c\u5185\u5bb9\n        title = item.find('title').text\n        content = item.find('content:encoded', namespaces)\n\n        if title is None or content is None:\n            continue\n\n        # \u8f6c\u4e49HTML\u7279\u6b8a\u5b57\u7b26\n        safe_title = escape(title)\n        safe_content = content.text if content.text else ''\n\n        # \u6784\u5efaHTML\u7ed3\u6784\n        html = f'''&lt;div class=\"post\"&gt;\n    &lt;h1&gt;{safe_title}&lt;\/h1&gt;\n    {safe_content}\n&lt;\/div&gt;'''\n\n        # \u751f\u6210\u6587\u4ef6\u540d\u5e76\u4fdd\u5b58\n        filename = sanitize_filename(title)\n        output_path = os.path.join(output_dir, filename)\n\n        with open(output_path, 'w', encoding='utf-8') as f:\n            f.write(html)\n\nif __name__ == '__main__':\n    # \u4f7f\u7528\u793a\u4f8b\n    process_xml('WordPress.2025-03-25.xml', 'test')\n<\/code><\/pre>\n<\/div>\n<h3 id=\"wiz-toc-0-920021758\">\u7a0b\u5e8f\u8bf4\u660e\uff1a<\/h3>\n<ol class=\"wiz-list-level1\">\n<li><strong>XML\u89e3\u6790<\/strong>\uff1a\u4f7f\u7528ElementTree\u89e3\u6790XML\u6587\u6863\uff0c\u5904\u7406WordPress\u7684\u547d\u540d\u7a7a\u95f4<\/li>\n<li><strong>\u5185\u5bb9\u8fc7\u6ee4<\/strong>\uff1a\u53ea\u5904\u7406\u6587\u7ae0\u7c7b\u578b(post)\u7684\u6761\u76ee<\/li>\n<li><strong>HTML\u751f\u6210<\/strong>\uff1a<br \/>\n\u2022 \u81ea\u52a8\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\u9632\u6b62XSS<br \/>\n\u2022 \u5305\u88f9\u5728<\/p>\n<div class=\"post\">\u5bb9\u5668\u4e2d<br \/>\n\u2022 \u4fdd\u7559\u539f\u59cbHTML\u683c\u5f0f\u5185\u5bb9<\/div>\n<\/li>\n<li><strong>\u6587\u4ef6\u4fdd\u5b58<\/strong>\uff1a<br \/>\n\u2022 \u6587\u4ef6\u540d\u57fa\u4e8e\u6807\u9898\u81ea\u52a8\u751f\u6210<br \/>\n\u2022 \u81ea\u52a8\u5904\u7406\u975e\u6cd5\u5b57\u7b26\u548c\u957f\u5ea6\u9650\u5236<br \/>\n\u2022 \u4fdd\u5b58\u5230\u6307\u5b9a\u8f93\u51fa\u76ee\u5f55<\/li>\n<\/ol>\n<h3 id=\"wiz-toc-1-950292380\">\u4f7f\u7528\u6b65\u9aa4\uff1a<\/h3>\n<ol class=\"wiz-list-level1\">\n<li>\u5c06wordpres\u4e0b\u8f7d\u7684xml\u6587\u4ef6\u4fdd\u5b58<code>WordPress.2025-03-25.xml<\/code><\/li>\n<li>\u8fd0\u884c\u811a\u672c\uff0c\u81ea\u52a8\u521b\u5efatest\u76ee\u5f55<\/li>\n<li>\u6bcf\u4e2a\u6587\u7ae0\u4f1a\u751f\u6210\u5355\u72ec\u7684HTML\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f\u4e3a<code>\u5904\u7406\u540e\u7684\u6807\u9898.html<\/code><\/li>\n<\/ol>\n<h3 id=\"wiz-toc-2-870894564\">\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n<ul>\n<li>\u9700\u8981Python 3.6+\u73af\u5883<\/li>\n<li>\u4fdd\u7559\u539f\u59cbHTML\u683c\u5f0f\u4e2d\u7684\u56fe\u7247\u94fe\u63a5\u7b49\u8d44\u6e90<\/li>\n<li>\u81ea\u52a8\u5904\u7406\u4e2d\/\u82f1\u6587\u5b57\u7b26\u548c\u7a7a\u683c<\/li>\n<li>\u8f93\u51fa\u76ee\u5f55\u4f1a\u81ea\u52a8\u521b\u5efa\uff0c\u65e0\u9700\u624b\u52a8\u5efa\u7acb<\/li>\n<\/ul>\n<p>\u8be5\u7a0b\u5e8f\u80fd\u591f\u9ad8\u6548\u5904\u7406WordPress\u5bfc\u51fa\u7684XML\u6587\u6863\uff0c\u81ea\u52a8\u5b8c\u6210\u683c\u5f0f\u8f6c\u6362\u3001\u5185\u5bb9\u63d0\u53d6\u548c\u6587\u4ef6\u5206\u5272\u4fdd\u5b58\u7b49\u64cd\u4f5c\u3002<\/p>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u5b9e\u73b0\u5c06XML\u6587\u6863\u8f6c\u6362\u4e3aHTML\u5e76\u63d0\u53d6\u7279\u5b9a\u5185\u5bb9\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u7f16\u5199Python\u7a0b\u5e8f\uff1a import xml. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,115,116],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-114","category-115","category-116"],"_links":{"self":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":3,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions\/622"}],"wp:attachment":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}