【问题标题】:How do I fetch a webpage with node-fetch?如何使用 node-fetch 获取网页?
【发布时间】:2021-11-26 18:53:09
【问题描述】:

以下代码:

import fetch from 'node-fetch'
;(async function fn() {
  const response = await fetch('https://learnitmyway.com/learning-material-software-development/')
  const data = await response.json()
  console.log({ data })
})()

结果:UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token < in JSON at position 0

【问题讨论】:

标签: javascript node.js fetch node-fetch


【解决方案1】:

json 替换为text,如下所示:

import fetch from 'node-fetch'
;(async function fn() {
  const response = await fetch('https://learnitmyway.com/learning-material-software-development/')
  const data = await response.text()
  console.log({ data })
})()

【讨论】:

  • 这可能是正确的,但没有像您指出的那样进行研究,也没有向社区添加任何内容。 @epascarello。
猜你喜欢
  • 2022-06-12
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 2017-12-14
  • 2021-04-28
  • 2019-01-29
  • 2021-04-11
  • 2019-05-18
相关资源
最近更新 更多