TypeError:无法在“节点”上执行“ removeChild”:参数1的类型不是“节点”

时间:2018-08-15 15:30:08

标签: javascript html

此错误的可能来源是什么?关于我要实现的目标的简要说明。我找到了一个epub抓取工具,该工具可以下载网页并将网页内容抓取到epub中。我尝试编辑js,但遇到此错误。以前,我成功下载了1个epub,但现在它停留在第9章。

堆栈跟踪错误:

TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.
        at Object.convert (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\node_modules\jsdom\lib\jsdom\living\generated\Node.js:749:11)
        at HTMLDivElement.removeChild (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\node_modules\jsdom\lib\jsdom\living\generated\Node.js:323:29)
        at getBodyXML (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:60:15)
        at getChapterString (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:40:16)
        at convertChapter (C:\Users\T\AppData\Roaming\npm\node_modules\worm-scraper\lib\convert.js:27:18)
        at <anonymous>

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

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,并将已编辑的刮板上载here

0 个答案:

没有答案