首页 > 解决方案 > TypeError:无法在“节点”上执行“removeChild”:参数 1 不是“节点”类型

问题描述

此错误的可能来源是什么?我要实现的目标的简要背景。我找到了一个 epub 刮板,它可以下载网页并将网页的内容刮到 epub 中。我试图编辑js,但遇到了这个错误。以前,我成功下载了 1 个 epub,但现在卡在第 9 章。

堆栈跟踪错误:

TypeError:无法在“节点”上执行“removeChild”:参数 1 不是“节点”类型。
        在 Object.convert (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\node_modules\jsdom\lib\jsdom\living\generated\Node.js:749:11)
        在 HTMLDivElement.removeChild (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\node_modules\jsdom\lib\jsdom\living\generated\Node.js:323:29)
        在 getBodyXML (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:60:15)
        在 getChapterString (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:40:16)
        在 convertChapter (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:27:18)
        在 <匿名>

我认为导致问题的代码是:

function getBodyXML(chapter, contentEl) {


 // Remove initial Next Chapter and Previous Chapter <p>
  //contentEl.removeChild(contentEl.firstElementChild);

  // Remove everything after the last <p> (e.g. analytics <div>s)
  const lastP = contentEl.querySelector("p:last-of-type");
  while (contentEl.lastElementChild !== lastP) {
    contentEl.removeChild(contentEl.lastElementChild);
  }

  // Remove empty <p>s or Last Chapter/Next Chapter <p>s
  while (isEmptyOrGarbage(contentEl.lastElementChild)) {
    contentEl.removeChild(contentEl.lastElementChild);
  }

  // Remove redundant attributes and style
  Array.prototype.forEach.call(contentEl.children, child => {
    if (child.getAttribute("dir") === "ltr") {
      child.removeAttribute("dir");
    }

    // Only ever appears with align="LEFT" (useless) or align="CENTER" overridden by style="text-align: left;" (also
    // useless)
    child.removeAttribute("align");

    if (child.getAttribute("style") === "text-align:left;") {
      child.removeAttribute("style");
    }
    if (child.getAttribute("style") === "text-align:left;padding-left:30px;") {
      child.setAttribute("style", "padding-left:30px;");
    }
  });

如果有帮助,将调用原始刮板并在此处worm-scraper上传编辑后的版本

标签: javascripthtml

解决方案


推荐阅读