保密观 知识问答题目下载

使用浏览器F12 在控制台输入以下代码:

(async function () {
  // ====== 配置 ======
  const API = 'https://www.baomi.org.cn/portal/main-api/spc/cms/publish/listByCat.do';
  const PAGE_SIZE = 50;        // 每页条数,越大请求次数越少
  const MAX_PAGES = 40;        // 最多翻多少页(1402/50≈29,设40留余量)
  // ==================

  const allItems = [];
  let pageNo = 1;
  let hasMore = true;

  while (hasMore && pageNo <= MAX_PAGES) {
    const url = `${API}?siteId=95&colCode=ZHISHIWENDA&pageNo=${pageNo}&pageSize=${PAGE_SIZE}&catId=`;
    console.log(`正在抓取第 ${pageNo} 页...`);

    try {
      const resp = await fetch(url, { credentials: 'include' });
      const json = await resp.json();

      if (!json.success || !json.result || json.result.length === 0) {
        console.log('没有更多数据了');
        break;
      }

      json.result.forEach(item => {
        const q = (item.SYS_TOPIC || item.WTDAAN || item.docName || '').trim();
        const a = (item.ZHISHIWENDA_WTDAAN || item.WTDAAN || '').trim();
        if (q || a) {
          allItems.push({ question: q, answer: a });
        }
      });

      console.log(`第 ${pageNo} 页完成,累计 ${allItems.length} 条`);

      if (json.result.length < PAGE_SIZE) {
        hasMore = false;
      } else {
        pageNo++;
      }

      // 随机延时,避免请求过快
      await new Promise(r => setTimeout(r, 800 + Math.random() * 700));

    } catch (e) {
      console.error(`第 ${pageNo} 页失败:`, e);
      await new Promise(r => setTimeout(r, 3000));
      pageNo++; // 跳过失败页继续
    }
  }

  console.log(`抓取结束,共 ${allItems.length} 条`);

  // ====== 生成 Word 兼容的 .doc 文件 ======
  function escapeHtml(s) {
    return String(s).replace(/[&<>"']/g, m => ({
      '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'
    }[m]));
  }

  let html = `
    <html xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:w="urn:schemas-microsoft-com:office:word"
          xmlns="http://www.w3.org/TR/REC-html40">
    <head>
      <meta charset="utf-8">
      <title>保密知识问答汇编</title>
      <style>
        body { font-family: "微软雅黑", "Microsoft YaHei", sans-serif; font-size: 11pt; line-height: 1.6; }
        h1 { text-align: center; font-size: 18pt; }
        .meta { text-align: center; color: #666; margin-bottom: 30px; }
        .q { font-weight: bold; color: #0066cc; font-size: 12pt; margin-bottom: 6px; }
        .a { margin-left: 24px; margin-bottom: 20px; }
        hr { border: none; border-top: 1px solid #ddd; margin: 18px 0; }
      </style>
    </head>
    <body>
      <h1>中国保密在线 · 知识问答汇编</h1>
      <p class="meta">共 ${allItems.length} 条</p>
  `;

  allItems.forEach((item, i) => {
    html += `
      <div>
        <p class="q">Q${i + 1}:${escapeHtml(item.question)}</p>
        <p class="a">${escapeHtml(item.answer).replace(/\n/g, '<br>')}</p>
      </div>
      <hr>
    `;
  });

  html += `</body></html>`;

  const blob = new Blob(['\ufeff', html], { type: 'application/msword' });
  const a = document.createElement('a');
  a.href = URL.createObjectURL(blob);
  a.download = '保密知识问答汇编.doc';
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(a.href);

  console.log('✅ 已生成 Word 文档并触发下载');
})();

 

posted @ 2026-09-29 14:02  擎天柱的舍利子  阅读(7)  评论(0)    收藏  举报