【问题标题】:Compare the HTML of 2 pages using fetch使用 fetch 比较 2 个页面的 HTML
【发布时间】:2020-07-12 01:36:32
【问题描述】:

我正在尝试比较 2 个 URL,它们应该是相同的,但是由于某种原因,我得到了一些差异。我想知道我是否对 NPM fetch 有一些基本的误解。

这是我的示例代码:

const fetch = require('node-fetch');

let startingPageBase = await fetch('https://www.example.com')
let startingPage = await startingPageBase.text()

let currentPageBase = await fetch('https://www.example.com')
let currentPage = await currentPageBase.text()

if (startingPageBase === currentPageBase) {
   console.log('Checked ')
} else {
   console.log('Not the same')
}

我基本上是在尝试检查 2 个页面的 HTML 是否相同。

【问题讨论】:

  • 另外,不要忘记将您的 awaits 包装到异步函数中。你不能在异步函数之外使用await,但是你的浏览器控制台或IDE可能已经告诉你了。

标签: javascript node.js node-fetch


【解决方案1】:

您是在比较两个承诺,而不是实际的文本值。

这样做:

if (startingPage === currentPage) {
  console.log('Checked ')
} else {
  console.log('Not the same')
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2022-12-31
    • 1970-01-01
    相关资源
    最近更新 更多