首页 > 解决方案 > 如何获取 URL 的 HTML

问题描述

我想将 URL 的 HTML 内容检索到一个字符串中,以便我可以使用str.search('XXX'),如果 XXX 在那里,我将执行一个操作。

     if (fetch("https://steamcommunity.com/id/" + message.channel.lastMessage)
                        .then(res => res.text())
                        .then(body => console.log(body))
                                    // I NEED TO TRANSFORM THE HTML INTO STRING AND USE str.search('The specified profile could not be found.'))

                {
                    message.channel.lastMessage.edit('URL "' + message.channel.lastMessage + '" is not taken.')
                }


    else {return}

这就是我目前所拥有的,欢迎任何其他实现相同目标的选择。

编辑2:

我按照某人的建议运行了将 res.text() 替换为 console.log(res) 的代码,这是输出:

$ node index.js
Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      readable: true,
      _events: [Object],
      _eventsCount: 7,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      _transformState: [Object],
      _hadError: false,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 88 e2 7c 69 2e 7f 00 00 e0 35 f4 02 00 00 00 00 b0 d8 eb 02 00 00 00 00 b0 d8 eb 02 00 00 00 00 67 22 2c 20 22 73 68 61 72 64 73 22 3a 20 31 2c 20 22 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _nextFlush: -1,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _level: -1,
      _strategy: 0
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://stackoverflow.com/questions/51120073/how-to-send-a-message-to-a-specific-channel',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object] },
    counter: 0
  }
}
undefined

这不是整个 HTML 正文,但它告诉我页面的状态,所以如果我能找到一种在状态为 404 时执行操作的方法,那将是我试图实现的同一件事。

标签: node.jsdiscord.js

解决方案


推荐阅读